gpt4 book ai didi

java - NullPointerExcetion native 方法访问器...哈希词问题

转载 作者:行者123 更新时间:2023-12-01 07:13:49 25 4
gpt4 key购买 nike

我正在编写一个读取文件并对“单词”进行排序的项目。这段代码编译正确,但随后它给了我一个空指针异常。有什么想法吗?

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:27‌​1)

最佳答案

这里有两个不同的问题:1.你的main方法不是静态的。2. 您使用的 IDE DrJava 没有显示良好的错误消息。

对于问题1,如果在words、addWord、main的声明中添加static,就可以运行程序了。

错误消息(问题 2)是 DrJava 的“运行”命令中的错误造成的,该命令应该能够运行 Java 程序和 Java 小程序。为了解决 2.,我已提交 bug report在 DrJava 的 SourceForge 页面。我们将尽快修复该错误。

对于给您带来的不便,我们深表歉意。

关于java - NullPointerExcetion native 方法访问器...哈希词问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7911004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com