aspose file tools*
The moose likes Beginning Java and the fly likes Netbeans IDE 6.1 Compilation fail Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Netbeans IDE 6.1 Compilation fail" Watch "Netbeans IDE 6.1 Compilation fail" New topic
Author

Netbeans IDE 6.1 Compilation fail

john smith
Greenhorn

Joined: Jun 04, 2008
Posts: 14
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

Joined: Aug 05, 2005
Posts: 3169
    
  10
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
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

Joined: Mar 10, 2008
Posts: 326

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Netbeans IDE 6.1 Compilation fail
 
Similar Threads
main() method mystery
errors in compression & release
declaring throws Exception after main()
Brand Newbie having Scanner Problem
Runtime Exceptions for JDBC program using Derby Client in Netbeans