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.
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 ]
Joanne
john smith
Greenhorn
Joined: Jun 04, 2008
Posts: 14
posted
0
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)