File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Netbeans IDE 6.1 Compilation fail

 
john smith
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The below program in Netbeans IDE compiles fine, but it does not want to run. It gives a NullPointerException. I'm very confused. Advice would be much appreciated. Thanks in advnce.



*****************************Program Start**********

package javaapplication2;
import java.io.*;
public class MyConsole {
static String name;
public static void main(String[] args){
Console c = System.console();
name = c.readLine("Name?");
System.out.println(name);
}
}

*****************************Program End**********




*****************************Netbeans Message Start**********

init:
deps-jar:
compile-single:
run-single:
Exception in thread "main" java.lang.NullPointerException
at javaapplication2.MyConsole.main(MyConsole.java:20)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

*****************************Netbeans Message End**********
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error message says the NPE is happening on line 20 of MyConsole.java, but there only appears to be 10 lines in the file. If you removed blank lines/comments from the file before posting it, can you tell us which is line 20 ?

Edit - having just read the javadoc for the Console class, it's probably due to you running the program from Netbeans. Try running it from a command line and it should work okay.
[ September 01, 2008: Message edited by: Joanne Neal ]
 
john smith
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've done a new compile, copy and paste. The Exception points to:

name = c.readLine("Name?");


******Program start******

package javaapplication2;
import java.io.*;
public class MyConsole {
static String name;
public static void main(String[] args){
Console c = System.console();
name = c.readLine("Name?");
System.out.println(name);
}
}




init:
deps-jar:
compile-single:
run-single:
Exception in thread "main" java.lang.NullPointerException
at javaapplication2.MyConsole.main(MyConsole.java:7)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Netbeans does not expose it's internal Console to the JVM. If you want to use the Console, you have to run it outside of the IDE.

The line:

assigns null to c since the JVM does not have any console registered to it.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic