File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Need some help, pleeaaase!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need some help, pleeaaase!!" Watch "Need some help, pleeaaase!!" New topic

Need some help, pleeaaase!!


Joined: Nov 15, 2001
Posts: 1
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.

(code tags added by Marilyn for readability)
[This message has been edited by Marilyn deQueiroz (edited November 15, 2001).]
Brian MacDonald

Joined: Nov 15, 2001
Posts: 4
I've just realised that my username was incorrect
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Brian could you edit your post and put code tag around it.
Brian MacDonald

Joined: Nov 15, 2001
Posts: 4
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.
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Look at player1, player2 and name. Look where you create instance variables and then where you use new. See anything wrong?
Brian MacDonald

Joined: Nov 15, 2001
Posts: 4
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.
[This message has been edited by Brian MacDonald (edited November 15, 2001).]
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
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
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
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.
Brian MacDonald

Joined: Nov 15, 2001
Posts: 4
Thank you.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Need some help, pleeaaase!!
Similar Threads
help with actionPerformed method.
Help with method
Loop for players
problem with program hanging
synchronization.. and threads !!!