使用StringReader
時出現異常。我創建對象時解析的字符串是通過String.split
生成的,它給我NullPointerException
。任何建議如何解決這個問題?StringReader從String.Split輸出中給出錯誤
下面的代碼:
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);
}
}
}
和文件讀屏類
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();
}
}
}
例外:
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結果:1
當我檢查線50在StringReader
類它包含
this.length = s.length();
用完整的堆棧跟蹤發佈您的異常。 –
@ PM77-1我不知道該怎麼做 –
是的,你知道,因爲你已經做到了。 –