3

I got an exception when using StringReader. The string that I parse when creating object was produced trough String.split, it gives me NullPointerException. Any suggestion how to fix this?

Here's the code:

public static void main(String[] args) throws IOException {
    // TODO code application logic here
    int jmldoc = 5;
    Hashmap hashsentences[][] = new Hashmap[5][100];
    Docreader reader = new Docreader();
    System.out.println(reader.doc[1]);

    for (int i = 0; i < jmldoc; i++) {
        int j = 0;
        while (reader.sentences[i][j] != null) {
            System.out.println(reader.sentences[i][j]);
            j++;              
            String temp=reader.sentences[i][j];
            StringReader h = new StringReader(temp);

        }
    }


}

and the docreader class

    public class Docreader {

    public String sentences[][]=new String[5][100];
    public String doc[]=new String[5];

    public Docreader() throws IOException{

    this.readdoc();
    this.splittosentence();

    }

   public void readdoc() throws IOException {
        for (int i = 0; i < 5; i++) {
            String temp = new String();
            temp = Docreader.readFile("datatrain/doc" + (i + 1) + ".txt");
            this.doc[i] = temp;
        }

    }

    public void splittosentence() {


        for (int i = 0; i < 5; i++) {
            String temp[];
            temp = doc[i].split("\\.");
            for(int j=0;j<temp.length;j++){
            sentences[i][j]=temp[j];

            }

        }

    }

    private static String readFile(String fileName) throws IOException {
        try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                sb.append("\n");
                line = br.readLine();
            }
            return sb.toString();
        }
    }
}

The exception:

Exception in thread "main" java.lang.NullPointerException
at java.io.StringReader.<init>(StringReader.java:50)
at stki_d_8_final.STKI_D_8_Final.main(STKI_D_8_Final.java:45)

Java Result: 1

And when I checked line 50 in StringReader Class it contain

this.length = s.length();
Luiggi Mendoza
  • 81,685
  • 14
  • 140
  • 306
jajamaharaja
  • 156
  • 12

2 Answers2

4

In this part of the code:

while (reader.sentences[i][j] != null) {
    System.out.println(reader.sentences[i][j]);
    j++;              
    String temp=reader.sentences[i][j];
    StringReader h = new StringReader(temp);
}

You're using j++ so the value of j increases by 1, then you have this code:

String temp=reader.sentences[i][j];

Since this new entry in the array hasn't been evaluated if is different from null, it may contain a null value, which is assigned to temp and thus initializing the StringReader with a null value as parameter.

A way to solve it would be increasing j after using it to build the StringReader. Also, in its current form, this code may also throw ArrayIndexOutOfBoundsException if all the values for reader.sentences[i] are not null. This would be a solution for code above:

while (j < reader.sentences[i].length && reader.sentences[i][j] != null) {
    System.out.println(reader.sentences[i][j]);
    String temp=reader.sentences[i][j];
    StringReader h = new StringReader(temp);
    j++;
}
Luiggi Mendoza
  • 81,685
  • 14
  • 140
  • 306
  • but one question, do not we check reader.sentences[i][j] != null if the element is null or not here? so why do we need to **recheck again?** – Kick Buttowski Jan 05 '15 at 03:16
  • 1
    @KickButtowski assume `i` and `j` have both a value of `0`. The while condition checks `while (reader.sentences[0][0] != null)` and pass this test, then `j++` executes and the initialization of `temp` will be `String temp = reader.sentences[0][1]`, which has not been evaluated before, and may contain a `null` value. Also, in its current form, this code may also throw `ArrayIndexOutOfBoundsException`. The easiest solution here would be using another `for` loop to control the value of `j`,instead of a `while`. – Luiggi Mendoza Jan 05 '15 at 03:19
3

In this part of your code:

while (reader.sentences[i][j] != null) {
        System.out.println(reader.sentences[i][j]);
        j++;              
        String temp=reader.sentences[i][j];
        StringReader h = new StringReader(temp);
}

you appropriately make sure that reader.sentences[i][j] != null, however you then increment j (j++) and do not do a null check.

PM 77-1
  • 11,712
  • 18
  • 56
  • 99