• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why NullPointerException?

 
Kari Nordmann
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Im getting a NullPointerException at "hovedvindu.update_player_health();" and I dont understand why...
Can someone tell me?
 
Lorenz Baylon
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kari,

From your post,


private Main hovedvindu;

public Ticker() {
Main hovedvindu = new Main();
t.start();
}


looks like you have had your "hovedvindu" variable shadowed. What you have instantiated in your constructor is a constructor-local variable named "hovedvindu" and not the private instance variable "hovedvindu". Instead of declaring another "hovedvindu" in your constructor, try using
this.hovedvindu = new Main();
instead of
Main hovedvindu = new Main();


Hope this helps.

Regards,

Lhorenz
 
Kari Nordmann
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah thanks alot, that fixed it
Then I learned something new
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Passing "this" to a new timer up in the variable declarations makes me uncomfortable. You can get into some nasty problems passing "this" around before the constructor is complete. This example looks safe enough because you don't start the Timer until later, but just to form good habits, why not create the Timer right before you start it?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic