Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ryan Ramsdenr
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do you create an instance of Minecraft and assign that to the mc var?

 
Ryan Ramsdenr
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I don't understand. What do you mean by that?

I tried;

 
Keith Rainey
Ranch Hand
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm calling it by,



How should I be calling it?
 
Keith Rainey
Ranch Hand
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic