NullPointerExcetion 本机方法访问器...哈希词问题

发布于 2024-12-12 01:27:45 字数 1541 浏览 0 评论 0原文

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

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)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风吹过旳痕迹 2024-12-19 01:27:45

这里有两个不同的问题:
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.

染年凉城似染瑾 2024-12-19 01:27:45

这个 while 循环很奇怪:

while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    while (line != null) {
       String word = scanner.next();
       addWord(word, linecount);
    }
    linecount++;
}

如果您的输入文件是:

a
b

那么 scanner.nextLine() 将返回 a,然后 scanner. next() 将返回 b,因为 nextLine 返回下一个结束行分隔的字符串,而 next 返回下一个标记输入文件。这真的是你想要的吗?我建议尝试这个:

while (scanner.hasNextLine()) {{
    String word = scanner.nextLine();
    addWord(word, linecount);

    linecount++;
}

请记住,只有当每行只有一个单词时,这才有效。如果你想每行处理多个单词,它会稍微长一些:

while (scanner.hasNextLine()) {{
    String line = scanner.nextLine();

    Scanner lineScanner = new Scanner(line);
    while(lineScanner.hasNext()) {
        addWord(lineScanner.next(), linecount);
    }

    linecount++;
}

This whileloop 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++;
}
少年亿悲伤 2024-12-19 01:27:45

您在评论中发布了这一点:

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)

看起来您正在使用非标准 Java 编译器。尝试使用 Sun 或 IBM 的 javac 来编译它,看看它是否会给出不同的跟踪。如果确实如此,那么可能只是您所在大学的 javac 实现出现错误。

我提到这一点是因为使用 JavacCompiler 类对于代码的运行时执行是可疑的。

You posted this in a comment:

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)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文