-1

i keep getting this one error, and im unsure how to change the code to get rid of this error. nothing seems to work.what would be the way to get rid of this error?

import java.util.*;
import java.io.*;

public class string2 {
    public static String getInput(Scanner in) throws IOException {
        File dataFile = new File("data.txt");
        String input = null;

        if (!dataFile.exists()) {
            System.out.println("Error");
            System.exit(0);
        }
        return input;
    }   

    public static int getWordCount(String input) throws IOException {
        String[] result = input.split(" ");
        return result.length;
    }     

    public static void main(String[] args) throws IOException {
        String input = getInput(new Scanner(System.in)); 
        int counter = getWordCount(input);
        System.out.println("The number of words in this string (" + input + ") are: " + counter);
    }   
}



xception in thread "main" java.lang.NullPointerException
    at string2.getWordCount(string2.java:40)
    at string2.main(string2.java:51)
stealthjong
  • 9,992
  • 13
  • 42
  • 83
  • The error you're getting comes with a "stack trace", that tells you where in the code the error is happening, the exact line. Please tell us which line in the code you posted is giving you the error. – Simone Gianni Sep 15 '14 at 23:41
  • And by the way .. there is the part where you read the input from the file? – Simone Gianni Sep 15 '14 at 23:42
  • ok hold on just a minute – user3038275 Sep 15 '14 at 23:42
  • 2
    Look carefully at your input() method. Input is clearly not being assigned a value other than null. – nanofarad Sep 15 '14 at 23:42
  • 2
    Please spend some time to lean how to use your IDE's debugger. The error means that you are trying to access an object whose reference is null. – OldProgrammer Sep 15 '14 at 23:42
  • Exception in thread "main" java.lang.NullPointerException at string2.getWordCount(string2.java:38) at string2.main(string2.java:49) – user3038275 Sep 15 '14 at 23:42
  • You are not reading anything from the file data.txt, that's the problem. – Simone Gianni Sep 15 '14 at 23:43
  • 1
    Don't post additional informations like stack trace or any kind of code relevant to question in comment. Instead use [[edit]] option below your question. – Pshemo Sep 15 '14 at 23:43
  • Here is what your `getInput()` method does: Create a string and make it null. If `datafile` exists then return the string I just made which is null. – takendarkk Sep 15 '14 at 23:47

2 Answers2

0

You are not assigning a value to input.

You are passing a null pointer (you've set input to NULL) as your input, resulting in an error.

If you are reading data from the file, then set a Scanner to read from the file and place it into a String, array, or some sort of variable that you can iterate through.

wadda_wadda
  • 846
  • 1
  • 7
  • 26
0

In your getInput(Scanner in) method, you are not re-assigning user entered input value into the String variable called input. It is always be null. you are just returning the null value of input variable. you should add the below line before return statement "return input" .

input = in.nextLine();

Moni
  • 433
  • 3
  • 9