I keep getting a null pointer exception from the paint method. It seems that there is a null in the player.display, but I can't find it. any help would be great. Sorry, but the style seems not to have made the transition. Thanks B.
(code tags added by Marilyn for readability) [This message has been edited by Marilyn deQueiroz (edited November 15, 2001).]
Sorry, I am a complete beginner at this!!! Also, I forgot to say. This is an assignment so I wouldn't like help in the way of actual code. But of the life of me, I can't see where I've went wrong. Any help any one could give me would be great, thanks Sorry once again.
I'm not sure, we've only started the course, and the commands I'm using are the same as those in the book. Should I create instance variables outside init? I'll continue to muddle through. I'm getting the variable coming up as null now, so is definitely just a matter of the way I'm creating them. I've been at this all day (I'm Scottish!!!) and I've been stuck for a while now, I'm sure I'll get it. thanks [This message has been edited by Brian MacDonald (edited November 15, 2001).]
The problem is that you declared them outside of init - which is fine, but then you RE-declared them inside the init() method. This effectively gave you NEW variables local only to the init() method, which you then did stuff with. When init() was over - poof! the local variables are gone and now all that is left are the original variables which were never initialized with any values . If you just USE the variables - without redeclaring them (by naming the variable "type"), the code in init() will use the variabled declared outside. Player player1 = . . . // declares a variable player1 = . . . // uses a variable without redeclaring it [This message has been edited by Cindy Glass (edited November 15, 2001).]
"JavaRanch, where the deer and the Certified play" - David O'Meara
By the way Brian, very noble of you to only want a little help and not have someone else code it for you. In case you didn't get what Cindy pointed out. In your Player class you define name as an instance variable. Private String name; Within your constructor you do:
You created another variable called name used only within the constructor. So your name instance variable is null. That is why you get the error. The other methods attempted to use name (instance variable) which was still null. Just say name = playerName; Same goes for player1 and player2. I know you just wanted hints but I think now you might understand what happened.