0

I'm writing a program in which a user can create a rectangle by giving the upper left and lower right coordinates. In turn, my program should automatically set the upper right and lower left coordinates using the points the user enters.

Ex: User adds a rectangle with the points (5,25) for upper left and (20,10) lower right. So the upper right point would be (20,25) and the lower left point would be (5,10).

This is how I wrote that out in my Rectangle class:

Point upperLeft;
Point lowerRight;
Point upperRight;
Point lowerLeft;
public double area;
public double perim;

public Rectangle (Point upperLeft, Point lowerRight){
    this.upperLeft = upperLeft;
    this.lowerRight = lowerRight;
}

public Point getUpperLeft() {
    Point p = upperLeft;
    return p;
}

public void setUpperLeft(Point p) {
    this.upperLeft = p;
}

public Point getLowerRight() {
    Point p2 = lowerRight;
    return p2;

}

public void setLowerRight(Point p2) {
    this.lowerRight = p2;
}

public Point getUpperRight(){
    upperRight.setX(lowerRight.getX()); 
    upperRight.setY(upperLeft.getY());
    return upperRight;
}

public Point getLowerLeft(){
    lowerLeft.setX(upperLeft.getX());
    lowerLeft.setY(lowerRight.getY());
    return lowerLeft;
}

In my main class I created a Rectangle called r, and did a println of r.getLowerRight(). Works fine, returns (20,10) as expected.

However, if I try to print one of the points that the user does not input, for example upperRight, I get a null pointer exception for the first line of getUpperRight, which is:

upperRight.setX(lowerRight.getX()); 

If I print the value of lowerRight.getX() on its own, it works. But for some reason when I try to set the X of the upperRight point to be the same as that of the lowerRight point, it just doesn't work. This is the Point class:

private double x;
private double y;
private double area;
private double perim;


public Point(double x, double y){
    this.x = x;
    this.y = y;
}


public double getX() {
    return x;
}

public void setX(double x) {
    this.x = x;
}

public double getY() {
    return y;
}

public void setY(double y) {
    this.y = y;
}

public String id() {        
    shapeID++;
    String id = String.valueOf(shapeID);
    return id;
}

public double area() {
    area = 0;
    return area;
}

public double perimeter() {
    perim = 0;
    return perim;
}

public void hit(Point p) {

}

public String toString(){
    return "(" + x + "," + y + ")";
}

public static double distance(Point p1, Point p2){
    double distance = Math.sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
    return distance;
}


}

Another note: Even if I replace upperRight.setX(lowerRight.getX()) with upperRight.setX(20.0), the getUpperRight println will still give a null pointer exception, which makes me think the problem is with the upperRight.setX part of the statement.

edit: I really don't see how this is a duplicate of "what is a null pointer exception?" That's a much more general question. I know what it is, but I don't see how it applies here.

sam
  • 151
  • 1
  • 3
  • 15
  • 1
    You don't initialize `upperRight` and `lowerLeft`, so they are `null` and you can't call methods on them. You can initialize them to `new Point()` either when you declare them, or in the constructor. Or don't have them at all and let their getters return `new Point(lowerRight.getX(), upperLeft.getY())` etc. – Cinnam Oct 01 '15 at 00:01
  • So dumb of me. Thank you! – sam Oct 01 '15 at 00:04

0 Answers0