• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Why NullPointerException?

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Im getting a NullPointerException at "hovedvindu.update_player_health();" and I dont understand why...
Can someone tell me?
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah thanks alot, that fixed it
Then I learned something new
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
I brought this back from the farm where they grow the tiny ads:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic