-3

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);
    }
}
  • "I get a null pointer exception" -- Please post the stacktrace – Shreyos Adikari Apr 11 '14 at 18:16
  • 1
    What does `Rectangle wall = new Rectangle();` in your **constructor** do, given that you've already declared an instance variable of the same name? – Sotirios Delimanolis Apr 11 '14 at 18:16
  • Also post the code for wall.moveto() its possible that its trying to use some unintialized variable – Kakarot Apr 11 '14 at 18:18
  • 1
    Defining local variables in your member functions (e.g. constructor) shadowing class variables is a bad thing to do. It leads to many bugs, like here. – Deduplicator Apr 11 '14 at 18:19
  • Thanks for your help guys I've updated the question with some more info. Sortirios I think it just creates a rectangle for me the constructor seems to work ok. – Eric Colgan Apr 12 '14 at 23:05

1 Answers1

3

Your constructor does not touch any of your member variables top, base and wall, because local variables declared in your constructor shadow the member variables you probably want to initialise.

Thus, they stay null, and trying to call member functions on them in moveTo throws.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • +1 but you're speaking like an experienced programmer. I'll bet the OP's pulling out a dictionary right about now to understand what you just wrote... – Nico Apr 11 '14 at 18:25
  • I'm not exactly sure what your comment means Deduplicator. If you could break it down for me in lamens terms that would be great. – Eric Colgan Apr 13 '14 at 00:09
  • Java zeroes all object before calling the constructor. Your constructor should then set all member variables appropriately. Yours does not, because instead of assigning to your members, you define a local variable of the same name in your constructor and assign that. The local shadows the member, which thus stays zeroed, aka `null` for pointers. Using `null` where an object is expected leads to the `NullPointerException`. – Deduplicator Apr 13 '14 at 00:13
  • Thanks guys. I managed to fix the code and what you were saying now makes sense. The lines of code that fixed it for me were. this.top=new Ellipse(); etc.. I now know I was creating a local variable top that could now be seen outside that method. – Eric Colgan Apr 14 '14 at 09:18