Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StringReader gives error from String.Split output

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();
like image 836
jajamaharaja Avatar asked Apr 26 '26 23:04

jajamaharaja


1 Answers

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++;
}
like image 128
Luiggi Mendoza Avatar answered Apr 28 '26 13:04

Luiggi Mendoza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!