This is my first every java project.All was going well and my program compiled. I got a 3D looking object on my screen. But when I use my moveTo(int x,int y) method I get a null pointer exception. I've looked it up and it's obviously a huge issue for a lot of people. But I still can't grasp the concept of it and any of the fixes that worked for other people don't seem to fit my program. I have tried using if statements to check for null and stuff like that but I can't get the syntax right or something. for example the base is an ellipse , the ellipse class has a moveTo(x,y) method.but when I call on it using base.moveTo(x,y) im getting the error. Can anybody help? cylinder1.moveTo(8, 8); Exception occurred. java.lang.NullPointerException at Cylinder3D.moveTo(Cylinder3D.java:57) Is this what you ment by stack trace? Here is the code for the moveTo method in the rectangle class. public void moveTo(int x, int y) { xPosition = x; yPosition = y; makeVisible(); draw(); }
public class Cylinder3D
{
private int diameter;
private int height;
private Ellipse top;
private Ellipse base;
private Rectangle wall;
int xPosition;
int yPosition;
public Cylinder3D(int diameter, int height)
{
int x = 0;
int y = 0;
Ellipse top = new Ellipse();
int xdiameter=diameter;
int ydiameter=diameter/2;
int xPosition = 0;
int yPosition = 0;
top.setState(xdiameter,ydiameter,xPosition,yPosition,"black");
Ellipse base = new Ellipse();
int yPositionBase = yPosition + height;
base.setState(xdiameter,ydiameter,xPosition,yPositionBase,"black");
Rectangle wall = new Rectangle();
int xSideLenght = diameter;
int ySideLenght = height;
int yPositionWall = yPosition + (diameter/4);
wall.setState(xSideLenght,ySideLenght,xPosition,yPositionWall,"black");
//....
}
public void moveTo(int x, int y)
{
wall.moveTo(x,y); //here is where I get the error.
base.moveTo(x,y);
top.moveTo(x,y);
}
}