Question

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)
Was it helpful?

Solution

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++;
}

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top