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
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 3011
    
    9
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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Netbeans IDE 6.1 Compilation fail
 
Similar Threads
Runtime Exceptions for JDBC program using Derby Client in Netbeans
declaring throws Exception after main()
errors in compression & release
Brand Newbie having Scanner Problem
main() method mystery