NullPointerExcetion 本机方法访问器...哈希词问题
我正在编写一个读取文件并对“单词”进行排序的项目。这段代码编译正确,但随后它给了我一个空指针异常。有什么想法吗?
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Hashtable;
public class Lab {
Hashtable<String, Word> words = new Hashtable<String, Word>();
public void addWord(String s, int i) {
if (words.containsKey(s)) {
words.get(s).addOne();
words.get(s).addLine(i);
} else {
words.put(s, new Word(s));
words.get(s).addLine(i);
}
}
public void main(String[] args) {
System.out.println("HI");
File file = new File("s.txt");
int linecount = 1;
try {
Scanner scanner = new Scanner(file);
System.out.println("HUH");
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
while (line != null) {
String word = scanner.next();
addWord(word, linecount);
}
linecount++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
异常的堆栈跟踪是:
java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)
I am writing a project that reads a file and sorts "Words". This code compiles correctly, yet then it gives me a null pointer exception. Any Ideas?
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Hashtable;
public class Lab {
Hashtable<String, Word> words = new Hashtable<String, Word>();
public void addWord(String s, int i) {
if (words.containsKey(s)) {
words.get(s).addOne();
words.get(s).addLine(i);
} else {
words.put(s, new Word(s));
words.get(s).addLine(i);
}
}
public void main(String[] args) {
System.out.println("HI");
File file = new File("s.txt");
int linecount = 1;
try {
Scanner scanner = new Scanner(file);
System.out.println("HUH");
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
while (line != null) {
String word = scanner.next();
addWord(word, linecount);
}
linecount++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
The exception's stacktrace is:
java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里有两个不同的问题:
1.你的main方法不是静态的。
2. 您使用的 IDE DrJava 没有显示良好的错误消息。
对于问题1,如果你在words、addWord和main的声明中添加static,你的程序就可以运行了。
错误消息(问题 2)是由 DrJava 的“run”命令中的错误造成的,该命令应该能够运行 Java 程序和 Java applet。为了解决 2.,我已提交错误报告< /a> 位于 DrJava 的 SourceForge 页面。我们很快就会修复这个错误。
对于给您带来的不便,我们深表歉意。
There are two different problems here:
1. Your main method is not static.
2. DrJava, the IDE you are using, is not showing a good error message.
As to problem 1, if you add static to the declaration of words, addWord, and main, you will be able to run your program.
The bad error message, problem 2, is a result of a bug in DrJava's "run" command, which is supposed to be able to run both Java programs and Java applets. To address 2., I have filed a bug report at DrJava's SourceForge page. We'll fix that bug soon.
I'm sorry for the inconvenience.
这个 while 循环很奇怪:
如果您的输入文件是:
那么
scanner.nextLine()
将返回a
,然后scanner. next()
将返回b
,因为nextLine
返回下一个结束行分隔的字符串,而next
返回下一个标记输入文件。这真的是你想要的吗?我建议尝试这个:请记住,只有当每行只有一个单词时,这才有效。如果你想每行处理多个单词,它会稍微长一些:
This
while
loop is strange:If your input file is:
Then
scanner.nextLine()
would be returna
, thenscanner.next()
would returnb
, becausenextLine
returns the next end-line delimited String, andnext
returns the next token from the input file. Is this really what you want? I'd suggest trying this:Keep in mind that this would only work if there's only a word per line. If you want to handle multiple words per line, it'd be slightly longer:
您在评论中发布了这一点:
看起来您正在使用非标准 Java 编译器。尝试使用 Sun 或 IBM 的 javac 来编译它,看看它是否会给出不同的跟踪。如果确实如此,那么可能只是您所在大学的 javac 实现出现错误。
我提到这一点是因为使用
JavacCompiler
类对于代码的运行时执行是可疑的。You posted this in a comment:
It looks like you are using a non-standard Java compiler. Try compiling this with Sun's or IBM's javac to see if it gives you a different trace. If it does then it might just be an error with your university's implementation of javac.
I mention this as the use of the
JavacCompiler
class is suspicious for your code's runtime execution.