java.lang.NullPointerException - Help
Im trying to run this code i have written but cannot see whats wrong with it. i get the java.lang.NullPointerException error when running it.
Basically what im trying to do is to move a background x position by using a new method i have created in Dice class (moveBackground).
The code i am running is
di.moveBackground(); where di is the object name
can any one help as i havent got a clue as im new to java
Class Called Dice only part of code is pasted here
Code :
private Square background;
public Square getBackground()
{
return background;
}
public void moveBackground() /** this is what method i am trying to run */
{
background.setXPos(75);
}
Square class
Code :
public void setXPos(int x)
{
this.xPos = x;
this.update();
}
Thanks in advance
Re: java.lang.NullPointerException - Help
Is the background variable instantiated prior to calling moveBackground, for example in the constructor or using a setter?
Re: java.lang.NullPointerException - Help
yah i think the object background is not properly instantiated... i think thats the object that the compiler is reffering to..
try to make a constructor that will instantiate the object background as your new object for the square class
Code :
public <Class Name> () {
background = new Square();
}
Re: java.lang.NullPointerException - Help
not sure what you mean all my code is below
To get a dice with 1 spot i run the code:
Square s = new Square();
Circle c1 = new Circle();
Circle c2 = new Circle();
Circle c3 = new Circle();
Circle c4 = new Circle();
Circle c5 = new Circle();
Circle c6 = new Circle();
Circle c7 = new Circle();
Dice di = new Dice(s, c1, c2, c3, c4, c5, c6, c7);
then the idea would be to run:
di.moveBackground();
to move the square created above to the right by 75
Re: java.lang.NullPointerException - Help
You never set or create the background variable. Look at the constructor of the class Dice, there should be something like
background = bg;
or
background = new Square();
Re: java.lang.NullPointerException - Help
Quote:
Originally Posted by
copeg
You never set or create the background variable. Look at the constructor of the class Dice, there should be something like
background = bg;
or
background = new Square();
brilliant. thanks very much for this
Really appreciate it :)