0

I want to print a box into the Java console using Unicode characters, but for some reason it gives me this runtime error:

--------------------Configuration: PacManGame - JDK version 1.7.0_25 <Default> - <Default>--------------------
Exception in thread "main" java.lang.NullPointerException
    at Scoreboard.genTopLine(Scoreboard.java:35)
    at Scoreboard.<init>(Scoreboard.java:17)
    at PacManGame.main(PacManGame.java:36)

Process interrupted by user.

code: http://pastebin.com/nuAsH857

Does anyone know what's wrong?

Aido
  • 1,524
  • 3
  • 18
  • 41
  • Have you tried to debug yor program? Which value is null iin line 35 of class Scoreboard? – Jens May 31 '14 at 22:45
  • @Zavior Did you even read the code? I know it's null, but it's null even after I set it. – Aido May 31 '14 at 22:52
  • @Jens Did you even read the code? I know it's null, but it's null even after I set it. – Aido May 31 '14 at 22:52
  • @user3580294 I know what a null pointer is. – Aido May 31 '14 at 22:54
  • "and how do I fix it". A debugger would have served you well here. – awksp May 31 '14 at 22:54
  • @user3580294 You didn't look at my code either, did you? I set the variable, it shouldn't be null anymore. – Aido May 31 '14 at 22:55
  • I wasn't talking about the variable. And yes, I did look at your code. – awksp May 31 '14 at 22:56
  • Can you post some code which we could actually use to reproduce your problem? The only NPE I can get is the one from `System.console().writer()` because `System.console()` returns `null` if there is no console, like if application was executed with `javaw.exe` instead of `java.exe` (like most of IDEs does to run code). – Pshemo May 31 '14 at 22:58
  • @Pshemo http://pastebin.com/nuAsH857 is in the main post – Aido May 31 '14 at 22:59
  • I saw it, that is why I said I got NPE on `System.console().writer()`. What I meant is some code with `main` method which we could use to get exactly same exception you are seeing. For now I can't reproduce your problem so will not be able to help you. – Pshemo May 31 '14 at 23:00
  • @Pshemo oh. http://pastebin.com/GtnTJiqh – Aido May 31 '14 at 23:05
  • Actually I used this code to create previously mentioned NPE. When I run it using `javaw.exe` console wasn't created so `System.console()` returned `null` (as expected). When I run id via `java.exe` it worked fine, but I am using Java 8 so that may be somehow relevant :/ – Pshemo May 31 '14 at 23:30
  • @Pshemo I'm running it in JCreator – Aido May 31 '14 at 23:34

1 Answers1

0

topline is actually NOT null - it's perfectly fine.
The problem is the use of System.console() which returns null (and hence you get NPE on .writer()).

Use System.out.println() instead and it'll work just fine.

An explanation as to why System.console() doesn't work in your IDE can be found here

Community
  • 1
  • 1
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129