0

I have this problem where I am supposed to write this program where using a Point class to create points, then create a Rectangle class with methods like area,perimeter, and pointInside... My code is always resulting in a Null Pointer Exception and I don't know why.

class Point {
   int x, y;

   Point() {
      x = y = 0;
   }

   Point(int x0, int y0) {
      x = x0;
      y = y0;
   }

   public String toString() {
      return "(" + x + "," + y + ")";
   }
}
class Rectangle {
   Point p1,p2;


   Rectangle(int x1,int y1,int x2,int y2)
   {
     x1 = p1.x;
     y1 = p1.y;
     x2 = p2.x;
     y2 = p2.y;
   }
   Rectangle(Point p1,Point p2)
   {
      this.p1 = p1;
      this.p2 = p2;
   }


   public int area ()
   {
     int width = p2.x - p1.x;
     int height = p2.y - p1.y;
     int area = width * height;
     return area;
   }
   public int perimeter()
   {
     int side1 = p2.x - p1.x;
     int side2 = p2.y - p1.y;
     int perimeter = side1 + side2 + side1 + side2;
     return perimeter;
   }
   public boolean pointInside(Point p)
   {
     if ((p.x >= p1.x && p.x <= p2.x) && (p.y >= p1.y && p.y <= p2.y))
     {
       return true;
     } else {
       return false;
     }
   }

}

class TestRectangle {
   public static void main(String[] args) {
      Point a = new Point(1,1);
      Point b = new Point(2,2);
      Point c = new Point(3,4);
      Point d = new Point(8,2);

      Rectangle yellow  = new Rectangle(a, c);
      Rectangle orange  = new Rectangle(2, 3, 9, 6);
      Rectangle green    = new Rectangle(3, 4, 4, 5);
      Rectangle blue    = new Rectangle(5, 1, 6, 5);
      Rectangle red = new Rectangle(7, 3, 9, 5);

      System.out.println("Perimeter of the yellow rectangle = " + yellow.perimeter()); // 10
      System.out.println("Perimeter of the orange rectangle= " + orange.perimeter()); // 20

      System.out.println("Area of the yellow rectangle = " + yellow.area()); // 6
      System.out.println("Area of the orange rectangle = " + orange.area()); // 21

      System.out.println("Point B inside yellow? " + yellow.pointInside(b)); // true
      System.out.println("Point D inside yello? " + yellow.pointInside(d)); // false


   }

}
Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
miguelcog
  • 1
  • 1
  • `x1 = p1.x;` - What do you think is the value of `p1` in that constructor there? – Johannes Kuhn Apr 07 '20 at 22:34
  • Your constructor `Rectangle(int x1,int y1,int x2,int y2)` is totally wrong. Constructor is supposed to initialize class variables. You have it backwards. – PM 77-1 Apr 07 '20 at 22:36
  • `Rectangle(int x1,int y1,int x2,int y2)` needs to convert the input into two `Point`s and assign them to `p1` and `p2` – MadProgrammer Apr 07 '20 at 22:37

1 Answers1

1

When something calls this constructor

Rectangle(int x1,int y1,int x2,int y2)
   {
     x1 = p1.x;
     y1 = p1.y;
     x2 = p2.x;
     y2 = p2.y;
   }

your variables p1 and p2 haven't been initialized to anything, since before that, all you do is declare those two fields, not actually set them to anything. They are null by default.

What you need to do is

Rectangle(int x1, int y1, int x2, int y2) {
  p1 = new Point(x1, y1);
  p2 = new Point(x2, y2);
}

I also have no idea why you're modifying the arguments to the constructor in the original.

user
  • 7,435
  • 3
  • 14
  • 44