• Post Reply Bookmark Topic Watch Topic
  • New Topic

system.out.println returning nullpointer exception

 
craig peddie
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have just downloaded jdk8 to start playing with javafx. Before I even trying any real javafx, I can't even get system.out.println to work. Any time I call it, it returns a null pointer exception. Even the simple hello world below bombs when you press the button to output the string:



Any ideas on how to fix this? Download and re-install java?

Thanks.....
 
Ron McLeod
Saloon Keeper
Posts: 1263
131
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How did you compile your application, and how did you run it?

Can you post the stack trace which shows the NullPointerException?
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot get a null exception from System.out.println unless you have reassigned out to a non-existent Stream. How are you passing the Stage reference? Insert this line as the first line of that method:-
System.out.printf("Stage actually exists: %b%n", stage);
 
craig peddie
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No stack trace was available since no console output at all made it to the screen. Traced all the way to the error and it was happening in FileOutputStream.write(). Reinstalled JRE and now everything is working fine. Must have a been a bit error somewhere in the download or install.....probably due to sunspots.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15715
73
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How did you run your application - from the console window, or by packaging it in a jar and then double-clicking the jar?

I can imagine that if you run your application by double-clicking a jar, there will be no console window at all, and then System.out will be null, resulting in a NullPointerException when you try to call println() on it.
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:there will be no console window at all, and then System.out will be null, resulting in a NullPointerException when you try to call println() on it.

I think you're confused with System.console(). System.out by default should never be null. However, it is possible to set it to null using System.setOut... I actually expected a NullPointerException on the call to setOut, but null is actually allowed.

Now this is a JavaFX application, so perhaps in JavaFX System.out is null, but for Swing/AWT applications that can run by double-clicking a JAR file, System.out will not be null unless explicitly set to null.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!