Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error Message

 
S. Stacey
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
I am VERY new to Java and have come across this error message and I am not sure what it means or how to fix it.
I can complie both my class and test class and when I go to run the program I get the following error:
Exception in thread "main" java.lang.NullPointerException at Game.throwDie(Game.java:20)
at TestGame.main (TestGame.java:18)
Can some one please help me out?
Thanks
Stacey
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Stacey
can u please post ur code if its not much big? it says that u get NullPointerException at line no:18.
NullPointerException means that - "the code tried to invoke a method on a object reference which was null". if you are famaliar with C/C++ then you can consider this NullPointerException (NPE written in short most of the time for ease) as "NULL" pointer usage.
regards
maulin
 
S. Stacey
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my code for the class:

Any help would be greatly appreciated.
Thanks
Stacey
[ July 03, 2003: Message edited by: Stacey Young ]
[ July 03, 2003: Message edited by: Stacey Young ]
[ July 03, 2003: Message edited by: Stacey Young ]
[ July 03, 2003: Message edited by: Stacey Young ]
[ July 03, 2003: Message edited by: Stacey Young ]
[ July 04, 2003: Message edited by: Stacey Young ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stacey, if you place your code between UBB CODE tags, the formatting will be preserved and it will be a lot easier for all of us here to help you out.
hth,
bear
P.S. Welcome to the Ranch!
P.P.S. You can edit your previous post to add the code tags.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem is that you are "hiding" your generator in your constructor. Instead of

use

The difference? You have already declared generator as a class member; by including the type again, you are telling the compiler that you are creating a reference that is local to the constructor. When you create an object for that reference, you are not creating one for the class reference. That is never assigned a value, so it takes the default, which is null. (While the "this." is technically optional in my example above, I placed it there to help show the concept of local vs. class reference)
 
S. Stacey
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WOO HOOO!!! that worked, THanks Joel!!!

and Bear I am not sure what you mean by the UBB CODE tag....can you explain it a little more to me.
 
Larry Cha Cha
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nd Bear I am not sure what you mean by the UBB CODE tag

When replying or creating a new topic there are buttons below the Instant UBB Code label, one of which is CODE. It simply adds an open and close tags which you should place your code in to format it correctly.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stacy, welcome to JavaRanch!
Take a look at the UBB Code explanation document for a good idea of what Bear was talking about.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic