StringReader gives error from String.Split output
When using StringReader
I got an exception. The string I parse when creating the object was created through 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();
}
}
}
An 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 the StringReader
class it contains
this.length = s.length();
source to share
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 are using j++
, so the value is j
incremented by 1, then you have this code:
String temp=reader.sentences[i][j];
Since this new entry in the array has not been evaluated, if it is different from null
, it may contain a value null
that is assigned temp
and thus initialized StringReader
with a null
value as a parameter.
The way to solve it will increase j
after using it to build StringReader
. Additionally, in its current form, this code can also throw ArrayIndexOutOfBoundsException
if all values ββfor reader.sentences[i]
are not null
. This would be the solution for the 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++;
}
source to share
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 have to make sure that reader.sentences[i][j] != null
, but then increment j ( j++
) and do a non null check.
source to share