• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception in thread "main" java.lang.NullPointerException

 
Garrett Nickel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code compiles just fine, but when I try to run the Launcher, it gives me the message:

Exception in thread "main" java.lang.NullPointerException
at HundredGame.startGame(HundredGame.java:19)
at GameLaunch.main(HundredGame.java:79)

my code is:



Please help, I'm very new to java
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
p1 - p4 are declared, but never initialized. Therefore they are null, and the code fails with a NullPointerException. You must initialize all variables before you use them, i.e.:

p1 = new Player();
 
Henry Wong
author
Marshal
Pie
Posts: 21427
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exception in thread "main" java.lang.NullPointerException
at HundredGame.startGame(HundredGame.java:19)
at GameLaunch.main(HundredGame.java:79)


The stacktrace gives the full details. On line 19 of the HundredGame.java file, in the startGame() method is code, which when called gives a null pointer exception. A null pointer exception is generally thrown when you are trying to dereference (ie. use) an object which hasn't been instantiated.

Henry
 
Garrett Nickel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, it works now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic