aspose file tools*
The moose likes Beginning Java and the fly likes NullPointerException I dont understand how to fix, Been working on it for 6 hours now Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NullPointerException I dont understand how to fix, Been working on it for 6 hours now" Watch "NullPointerException I dont understand how to fix, Been working on it for 6 hours now" New topic
Author

NullPointerException I dont understand how to fix, Been working on it for 6 hours now

Ryan Ramsdenr
Greenhorn

Joined: Dec 03, 2011
Posts: 10
Argh, Im really annoyed. I've been working on an IRC client in a game i've been editing, and well. I just dont know how to solve this..

i'm getting nullpointerexception at "mc.thePlayer.addChatmessage(str);" (Line 65)



I'm not really sure what else you will need, So just ask!

If you help me solve this ill be very grateful!
Keith Rainey
Ranch Hand

Joined: Jan 19, 2011
Posts: 66

Where do you create an instance of Minecraft and assign that to the mc var?


Keith Rainey
OCPJP6
Ryan Ramsdenr
Greenhorn

Joined: Dec 03, 2011
Posts: 10
Sorry, I don't understand. What do you mean by that?

I tried;

Keith Rainey
Ranch Hand

Joined: Jan 19, 2011
Posts: 66

I was looking for a reason for the null pointer exception. The code you posted did not show any assignment to the mc variable.

If the object variable doesn't refer to an object and you try to access a member, you will get a NullPointerException.
Ryan Ramsdenr
Greenhorn

Joined: Dec 03, 2011
Posts: 10
Hmm, But how do I fix it? (Sorry for my lack of Understanding, I'm New to Java)
Also thankyou for your help so far!
Keith Rainey
Ranch Hand

Joined: Jan 19, 2011
Posts: 66





You need to verify that the the minecraft parameter is receiving an actual instance.

I will assume you are calling this particular constructor when you create a new IRC and not a no-argument constructor like new IRC()
Ryan Ramsdenr
Greenhorn

Joined: Dec 03, 2011
Posts: 10
I'm calling it by,



How should I be calling it?
Keith Rainey
Ranch Hand

Joined: Jan 19, 2011
Posts: 66

Ryan Ramsdenr wrote:I'm calling it by,



How should I be calling it?


I'm making some assumptions here based on the code fragments you've given.

Here you are making a call to a static method in the IRC class. Presumably mc.thePlayer.username is a String which you pass to the main(String name) method.
This is not instantiating an object of the IRC class. You are accessing the class members (static methods and variables).
The Public IRC() constructor you posted earlier does not execute except when you create a new instance. Nothing you've given shows that you are creating a new instance.
If the constructor doesn't run, then the mc variable doesn't get set.
If the mc variable doesn't get set, you get a NullPointerException when you try to access its contents (which don't exist).


Try throwing a couple of debugging lines into the constructor:





That will tell you whether or not that constructor is being called or not.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointerException I dont understand how to fix, Been working on it for 6 hours now