import easyIO.*;
class Oblig3A {
public static void main(String[] args) {
OrdAnalyse oA = new OrdAnalyse(args[0]);
oA.textLeser();
} //closes main
} //closes Oblig3 class
class OrdAnalyse {
String[] ord = new String[5000];
int[] antall = new int[5000];
int antUnik = 0;
String argu;
OrdAnalyse(String file){
argu = file;
} //args relay
void textLeser(){
In lesText = new In(argu);
Out nyText = new Out("oppsummering.txt" , true);
while (!lesText.endOfFile()) {
String reader = lesText.inWord();
for(int i=0; i<ord.length; i++) {
if(reader.equals(ord[i])) {
antall[i]++;
antUnik++;
ord[i] = reader;
nyText.out(ord[i]);
} //if
else {
antall[i]++;
} //else
} //for
}//while
}//textLeser
}//OrdAnalyse.class
有人能发现问题所在吗? :需要帮助。
我不知道它出了什么问题,我已经尝试了很多不同的方法。 :/
不知道为什么会这样:s
第 33 行 = if(reader.equals(ord[i])) {
最佳答案
just tried it with a different file and got this
Exception in thread "main" java.lang.NullPointerException at OrdAnalyse.textLeser(Oblig3A.java:33) at Oblig3A.main(Oblig3A.java:8)
根据您的评论,以下方法调用返回 null
。
String reader = lesText.inWord();
稍后,在第 33 行,您访问 reader
变量。
if(reader.equals(ord[i]))
在 null 对象上调用方法会引发您所见过的 NullPointerException
。
最有可能的是,inWord()
与 endOfFile()
结合使用无法按您的预期工作。您需要阅读这两种方法的文档,然后决定如何处理 inWord()
的 null
结果:最简单的解决方案是打破外部循环,但您可能会想做点别的事。
String reader = lesText.inWord();
if (reader == null) { break; }
关于java - 线程 "main"java.lang.NullPointerException 中出现异常(Uni work),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19245701/