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
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
Joined: Jul 03, 2003
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 ]
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.
Your problem is that you are "hiding" your generator in your constructor. Instead of
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)
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Joined: Jul 03, 2003
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.
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.