Search code examples
javajava.util.scannergcj

Scanner class in Java5 throw java.lang.NullPointerException


I am using scanner class in java5, and the following code will throw an exception:

Scanner scanner = new Scanner
        (new File(args[0]));
int dealId;
while (scanner.hasNextLine()) {
    dealId = scanner.nextInt();
    System.out.println(dealId);
}
scanner.close();

The stacktrace is:

Exception in thread "main" java.lang.NullPointerException
   at java.util.regex.Matcher.toMatchResult(libgcj.so.10)
   at java.util.Scanner.myCoreNext(libgcj.so.10)
   at java.util.Scanner.myPrepareForNext(libgcj.so.10)
   at java.util.Scanner.myNextLine(libgcj.so.10)
   at java.util.Scanner.hasNextLine(libgcj.so.10)

Does anybody knows what caused this exception?


Solution

  • The GCJ Home Page suggest it "supports most of the 1.4 libraries plus some 1.5 additions. "

    Scanner was added in version 1.5 and I suspect you have hit a piece of functionality GCJ doesn't support. You need to try something different to see what you can get to work.

    Is there any reason you are not using OpenJDK/Oracle Java 6 or 7? (Please don't say its for performance reasons ;)