Skip to main content
expanded explanation, add link to docs on primative types
Source Link
Pikalek
  • 13.4k
  • 5
  • 49
  • 54

In Java, every variable that is an object type (i.ethat is, any thing that are notisn't a primative type such as an int, float, boolean, etc..) is initialized to null. That means unless you store something in that variable via

 SomeObjectType variable = new SomeObjectType(); //or SomeObjectType variable = otherVariableThatIsNotNull; 

Then you will get a NPENullPointerException (NPE) whenever you call variable.anyFunction()

There may be more than one, but it looks like you have a guaranteed NPE in your WorldController class. You never give a value to your levScreen variable and you get a NPE when you call levScreen.getCurrLevel().

Either in WorldController's constructor or in the initLevel() function you need to pass in your levelScreen so that WorldController can store and access it.

In Java, every variable that is an object type (i.e any that are not int, float, boolean etc..) is initialized to null. That means unless you store something in that variable via

 SomeObjectType variable = new SomeObjectType(); //or SomeObjectType variable = otherVariableThatIsNotNull; 

Then you will get a NPE whenever you call variable.anyFunction()

There may be more than one, but it looks like you have a guaranteed NPE in your WorldController class. You never give a value to your levScreen variable and you get a NPE when you call levScreen.getCurrLevel().

Either in WorldController's constructor or in the initLevel() function you need to pass in your levelScreen so that WorldController can store and access it.

In Java, every variable that is an object type (that is, any thing that isn't a primative type such as an int, float, boolean, etc.) is initialized to null. That means unless you store something in that variable via

 SomeObjectType variable = new SomeObjectType(); //or SomeObjectType variable = otherVariableThatIsNotNull; 

Then you will get a NullPointerException (NPE) whenever you call variable.anyFunction()

There may be more than one, but it looks like you have a guaranteed NPE in your WorldController class. You never give a value to your levScreen variable and you get a NPE when you call levScreen.getCurrLevel().

Either in WorldController's constructor or in the initLevel() function you need to pass in your levelScreen so that WorldController can store and access it.

Source Link
spectacularbob
  • 1.4k
  • 1
  • 10
  • 12

In Java, every variable that is an object type (i.e any that are not int, float, boolean etc..) is initialized to null. That means unless you store something in that variable via

 SomeObjectType variable = new SomeObjectType(); //or SomeObjectType variable = otherVariableThatIsNotNull; 

Then you will get a NPE whenever you call variable.anyFunction()

There may be more than one, but it looks like you have a guaranteed NPE in your WorldController class. You never give a value to your levScreen variable and you get a NPE when you call levScreen.getCurrLevel().

Either in WorldController's constructor or in the initLevel() function you need to pass in your levelScreen so that WorldController can store and access it.