File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Why NullPointerException? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why NullPointerException?" Watch "Why NullPointerException?" New topic
Author

Why NullPointerException?

Kari Nordmann
Ranch Hand

Joined: Feb 12, 2007
Posts: 38

Im getting a NullPointerException at "hovedvindu.update_player_health();" and I dont understand why...
Can someone tell me?
Lorenz Baylon
Ranch Hand

Joined: Jan 04, 2006
Posts: 99
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


I just cant look, its killing me.
Kari Nordmann
Ranch Hand

Joined: Feb 12, 2007
Posts: 38
Ah thanks alot, that fixed it
Then I learned something new
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why NullPointerException?