Augie Mannin
Augie Mannin

Reputation: 11

How to fix "Cannot invoke "java.awt.Point.getX()" because "this.p" is null"

For an inheritance practice assignment, I have a class Square which extends rectangle, and here are the relevant methods:

public class Square extends Rectangle {
    private Point p;
    private int sideLength;
    public Square(Point p, int sideLength){
        super(p);
        this.sideLength = sideLength;
    }

    public String toString(){
        return getClass().getName() + "\nCenter point: (" + p.getX() + ',' + p.getY() + ") \nSide Length: " + sideLength + " \nArea: " + this.getArea() + " \nPerimeter: " + this.getPerimeter();
    }


}

there's also a getArea() and getPerimeter() method but neither of those are causing this issue. I have a separate class to test this one:

public class SquareTest {
    public static void main(String[] args){
        ArrayList<Square> squareList = new ArrayList<>();
        Point p1 = new Point(1, 1);
        Point p2 = new Point(1, 2);
        Point p3 = new Point(4, 1);
        Point p4 = new Point(2, 3);
        Point p5 = new Point(5, 4);
        Square one = new Square(p1, 1);
        Square two = new Square(p2, 2);
        Square three = new Square(p3, 4);
        Square four = new Square(p4, 4);
        Square five = new Square(p5,5);
        squareList.add(one);
        squareList.add(two);
        squareList.add(three);
        squareList.add(four);
        squareList.add(five);
        for (Square each : squareList){
            System.out.println(each.toString());
            System.out.println();
        }
    }
}

What it's supposed to do is print out the toString() result for each of the five square objects, where one square looks like this:

Square
Center point: (1,1)
Side Length: 1
Area: 1
Perimeter: 1

but instead i get the following runtime error:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.awt.Point.getX()" because "this.p" is null
    at Square.toString(Square.java:21)
    at SquareTest.main(SquareTest.java:23)

I'm confused as to why it thinks this.p is null when I've clearly instantiated five Point objects. Any ideas?

Upvotes: 1

Views: 1731

Answers (1)

Gautham M
Gautham M

Reputation: 4935

NullPointerException was caused because you haven't initialized the p in Square class. Yu have just passed it to the Rectangle constructor so it is the p in Rectangle class which is getting initialized.

Since you are not using the point object in your Square rather than just passing it to the super class, there is no need to declare a Point object in the Square class. If your Rectangle class has a getter method for Point p then, you could use that in the Square class.

Assuming that Rectangle class has a getter method for p, You could refactor your Square class to :

public class Square extends Rectangle {
    private int sideLength;
    public Square(Point p, int sideLength){
        super(p);
        this.sideLength = sideLength;
    }

    public String toString(){
        return getClass().getName() 
               + "\nCenter point: (" + getP().getX() + ',' + getP().getY()
               + ") \nSide Length: " + sideLength
               + " \nArea: " + this.getArea()
               + " \nPerimeter: " + this.getPerimeter();
    }
}

Upvotes: 4

Related Questions