NullPointerExcetion本机方法访问器...散列字问题 [英] NullPointerExcetion Native Method Accessor... Hashing Words Issue
问题描述
我正在写一个读取文件并对单词进行排序的项目。这段代码编译正确,然后它给了我一个空指针异常。任何想法?
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);
档案档案=新档案(s.txt);
int linecount = 1;
尝试{
扫描仪扫描仪=新的扫描仪(文件);
System.out.println(HUH);
while(scanner.hasNextLine()){
String line = scanner.nextLine();
while(line!= null){
String word = scanner.next();
addWord(word,linecount);
}
linecount ++;
$ b}
} catch(FileNotFoundException e){
e.printStackTrace();
异常的堆栈跟踪是:
java.lang.NullPointerException $ b $在sun.reflect.NativeMethodAccessorImpl.invoke0(本地方法)
在太阳.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)
while while loop is strange: code> while(scanner.hasNextLine()){
String line = scanner.nextLine();
while(line!= null){
String word = scanner.next();
addWord(word,linecount);
}
linecount ++;
$ / code>
如果您的输入文件是:
a
b
然后 scanner.nextLine()
会返回 a
,然后 scanner.next()$因为 nextLine
返回下一行以分隔符结束的字符串,并且<$ c $>会返回 b
c> next
从输入文件返回下一个标记。这真的是你想要的吗?我建议试试这个:
while(scanner.hasNextLine()){{
String word = scanner。 nextLine();
addWord(word,linecount);
linecount ++;
}
请记住,这只适用于每行只有一个字的情况。如果你想处理每行多个单词,它会稍微长一点:
while(scanner.hasNextLine()){ {
String line = scanner.nextLine();
扫描仪lineScanner =新扫描仪(线);
while(lineScanner.hasNext()){
addWord(lineScanner.next(),linecount);
}
linecount ++;
}
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)
解决方案 This while
loop is strange:
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
while (line != null) {
String word = scanner.next();
addWord(word, linecount);
}
linecount++;
}
If your input file is:
a
b
Then scanner.nextLine()
would be return a
, then scanner.next()
would return b
, because nextLine
returns the next end-line delimited String, and next
returns the next token from the input file. Is this really what you want? I'd suggest trying this:
while (scanner.hasNextLine()) {{
String word = scanner.nextLine();
addWord(word, linecount);
linecount++;
}
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:
while (scanner.hasNextLine()) {{
String line = scanner.nextLine();
Scanner lineScanner = new Scanner(line);
while(lineScanner.hasNext()) {
addWord(lineScanner.next(), linecount);
}
linecount++;
}
这篇关于NullPointerExcetion本机方法访问器...散列字问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文