Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java NullPointerException when calling from an abstract class

I am trying to simply extend an abstract class in Java and call a few methods stored in it. I keep getting NullPointerException when I do so. Am I missing something about abstraction here?

This is the parent class:

public abstract class Shape {
    public Color color;
    public Point center;
    public double rotation;

    public Shape() {
        Color color = new Color();
        Point center = new Point();
        rotation = 0.0;
        System.out.println("shape created");
    }

    public void setLocation( Point p ) { center.locationX = p.locationX; center.locationY = p.locationY; }
    public void setLocation( double x, double y ) { center.locationX = x; center.locationY = y; }

    public abstract double calcArea();
    public abstract boolean draw();
}

And the child class:

public class Ellipse extends Shape {        
    public Ellipse() {
    }

    public double calcArea() {
        return 0.0;
    }

    public boolean draw() {
        return true;
    }
}

You might want to see Point:

public class Point {
    public double locationX;
    public double locationY;

    public Point() {
        locationX = 0.0;
        locationY = 0.0;
    }
}

And finally the main function:

public class MakeShapes {
    public static void main(String []args) {
        Ellipse myShapes = new Ellipse();
        myShapes.setLocation( 100.0, 100.0 );
    }
}

As soon as I use the setLocation(), I get the NPE. Any thoughts? My brain hurts from trying to figure this out. Thanks!!!

like image 506
Scott McDermott Avatar asked Sep 21 '15 20:09

Scott McDermott


2 Answers

The problem here is that your Shape constructor creates a local Point reference called center and initilizes that one instead of intializing the field (and you have the same problem with color). Try like this:

public abstract class Shape {
    public Color color;
    public Point center;
    public double rotation;

    public Shape() {
        color = new Color(); //changed to intialize the field
        center = new Point(); //changed to intialize the field
        rotation = 0.0;
        System.out.println("shape created");
    }

    public void setLocation( Point p ) { center.locationX = p.locationX; center.locationY = p.locationY; }
    public void setLocation( double x, double y ) { center.locationX = x; center.locationY = y; }

    public abstract double calcArea();
    public abstract boolean draw();
}
like image 132
Keppil Avatar answered Nov 19 '22 11:11

Keppil


This is a subtle bug. You are creating a local center variable and not assigning it to this.center

    public Shape() {
        Color color = Color.BLACK;
        Point center = new Point();
        rotation = 0.0;
        System.out.println("shape created");
    }

change the color and center declaration to be to this.

this.center = new Point()

In the end this.center is never actually defined and hence the NPE.

like image 4
John Vint Avatar answered Nov 19 '22 10:11

John Vint