# Triangle class help

• 04-20-2011, 08:50 PM
Oconski
Triangle class help
Hi everyone,

For my beginner java class I need to implement a Triangle class that computes the length of all sides, angles, perimeter and area. In doing this everything seemed to work until I tried to use the application by inputing the x and y coordinates with which it gave me the error java.lang.NullPointerException: null. Now I have looked online and don't understand the description of this error because i've only been doing this for a few weeks. This is the line that shows the error:

Code:

`double length = p1.distance(p2);`
Here is my Triangle class and TriangleTester class:

Code:

```public class Triangle {   private int x0, y0, x1, y1, x2, y2;    private Point p0, p1, p2;                   /**     * Sets coordinates of each point of the triangle.     */   public Triangle()   {       Scanner scanObject = new Scanner(System.in);             System.out.println("Enter x coordinate for first point: ");       x0 = scanObject.nextInt();       System.out.println("Enter y coordinate for first point: ");       y0 = scanObject.nextInt();       System.out.println("Enter x coordinate for second point: ");       x1 = scanObject.nextInt();       System.out.println("Enter y coordinate for second point: ");       y1 = scanObject.nextInt();       System.out.println("Enter x coordinate for third point: ");       x2 = scanObject.nextInt();       System.out.println("Enter y coordinate for third point: ");       y2 = scanObject.nextInt();   }        /**     * Sets x and y coordinates to a specific point.     * @param x0 x value for p0.     * @param y0 y value for p0.     * @param x1 x value for p1.     * @param y1 y value for p1.     * @param x2 x value for p2.     * @param y2 y value for p2.     */   public Triangle(int x0, int y0, int x1, int y1, int x2, int y2)   {           Point p0 = new Point(x0, y0);       Point p1 = new Point(x1, y1);       Point p2 = new Point(x2, y2);             }     /**     * Sets the length of Side A.     * @return Side A.     */   public double getSideA()   {       double length = p1.distance(p2);       return length;   }        /**     * Sets the length of Side B.     * @return Side B.     */   public double getSideB()   {       double length = p0.distance(p2);       return length;   }     /**     * Sets the length of Side C.     * @return Side C.     */   public double getSideC()   {       double length = p0.distance(p1);       return length;   }   /**     * Sets the angle of Angle A.     * @return Angle A.     */   public double getAngleA()   {       double as = getSideA(), bs = getSideB(), cs = getSideC();       double angle = Math.acos((bs * bs + cs * cs - as * as) / (2 * bs * cs));       return angle;   }     /**     * Sets the angle of Angle B.     * @return Angle B.     */   public double getAngleB()   {       double as = getSideA(), bs = getSideB(), cs = getSideC();       double angle = Math.acos((as * as + cs * cs - bs * bs) / (2 * as * cs));       return angle;   }     /**     * Sets the angle of Angle C.     * @return Angle C.     */   public double getAngleC()   {       double as = getSideA(), bs = getSideB(), cs = getSideC();       double angle = Math.acos((as * as + bs * bs - cs * cs) / (2 * as * bs));       return angle;   }     /**     * gets the perimeter of the triangle.     * @return the perimeter of the triangle.     */   public double getPerimeter()   {       double as = getSideA(), bs = getSideB(), cs = getSideC();       return as + bs + cs;        }     /**     * Gets the signed area of the triangle.     * @return the signed area of the triangle.     */   double getSignedArea()   {       double signedArea = 0.5 * (p0.getX() * (p1.getY() - p2.getY())                               +  p1.getX() * (p2.getY() - p0.getY())                               +  p2.getX() * (p0.getY() - p1.getY()));       return signedArea;   }     /**     * gets the area of the triangle.     * @return the area of the triangle.     */   public double getArea()   {       return Math.abs(getSignedArea());   } }```
Code:

```public class TriangleTester {   /**   * Tests Triangle Class.   * @param args tests methods from trangle class.   */   public static void main(String [] args)   {       Triangle myTriangle = new Triangle();                 System.out.println("SideA: " + myTriangle.getSideA());       System.out.println("SideB: " + myTriangle.getSideB());       System.out.println("SideC: " + myTriangle.getSideC());       System.out.println("AngleA: " + myTriangle.getAngleA());       System.out.println("AngleB: " + myTriangle.getAngleB());       System.out.println("AngleC: " + myTriangle.getAngleC());       System.out.println("Perimeter: " + myTriangle.getPerimeter());       System.out.println("Area: " + myTriangle.getArea());   } }```
any help is greatly appreciated
• 04-20-2011, 09:07 PM
jojo
You are only setting p0, p1 and p2 in a NON DEFAULT constructor.
Your default constructor isn't creating any new objects that are using p0,p1 and p2 as reference. You are using new Triangle() so thats the default constructor.

Anyways nullpointerexception just means you didn't initialize an object variable

so put these

Quote:

Point p0 = new Point(x0, y0);
Point p1 = new Point(x1, y1);
Point p2 = new Point(x2, y2);
at the end of your default constructor and it should work fine
• 04-20-2011, 09:23 PM
Oconski
Ok so i put those at the end of the default constructor and I am still getting the null message.Maybe I didn't do exactly what you said but this is what I have now:

Code:

```private int x0, y0, x1, y1, x2, y2;    private Point p0, p1, p2;                   /**     * Sets coordinates of each point of the triangle.     */   public Triangle()   {       Scanner scanObject = new Scanner(System.in);             System.out.println("Enter x coordinate for first point: ");       x0 = scanObject.nextInt();       System.out.println("Enter y coordinate for first point: ");       y0 = scanObject.nextInt();       System.out.println("Enter x coordinate for second point: ");       x1 = scanObject.nextInt();       System.out.println("Enter y coordinate for second point: ");       y1 = scanObject.nextInt();       System.out.println("Enter x coordinate for third point: ");       x2 = scanObject.nextInt();       System.out.println("Enter y coordinate for third point: ");       y2 = scanObject.nextInt();             Point p0 = new Point(x0, y0);       Point p1 = new Point(x1, y1);       Point p2 = new Point(x2, y2);   }          /**     * Sets the length of Side A.     * @return Side A.     */   public double getSideA()   {       double length = p1.distance(p2);       return length;   }        /**     * Sets the length of Side B.     * @return Side B.     */   public double getSideB()   {       double length = p0.distance(p2);       return length;   }     /**     * Sets the length of Side C.     * @return Side C.     */   public double getSideC()   {       double length = p0.distance(p1);       return length;   }   /**     * Sets the angle of Angle A.     * @return Angle A.     */   public double getAngleA()   {       double as = getSideA(), bs = getSideB(), cs = getSideC();       double angle = Math.acos((bs * bs + cs * cs - as * as) / (2 * bs * cs));       return angle;   }     /**     * Sets the angle of Angle B.     * @return Angle B.     */   public double getAngleB()   {       double as = getSideA(), bs = getSideB(), cs = getSideC();       double angle = Math.acos((as * as + cs * cs - bs * bs) / (2 * as * cs));       return angle;   }     /**     * Sets the angle of Angle C.     * @return Angle C.     */   public double getAngleC()   {       double as = getSideA(), bs = getSideB(), cs = getSideC();       double angle = Math.acos((as * as + bs * bs - cs * cs) / (2 * as * bs));       return angle;   }     /**     * gets the perimeter of the triangle.     * @return the perimeter of the triangle.     */   public double getPerimeter()   {       double as = getSideA(), bs = getSideB(), cs = getSideC();       return as + bs + cs;        }     /**     * Gets the signed area of the triangle.     * @return the signed area of the triangle.     */   double getSignedArea()   {       double signedArea = 0.5 * (p0.getX() * (p1.getY() - p2.getY())                               +  p1.getX() * (p2.getY() - p0.getY())                               +  p2.getX() * (p0.getY() - p1.getY()));       return signedArea;   }     /**     * gets the area of the triangle.     * @return the area of the triangle.     */   public double getArea()   {       return Math.abs(getSignedArea());   } }```
• 04-20-2011, 09:24 PM
jojo
sorry, i gave you the wrong code

make sure u have in all constructors: (without the Point before it, or else you will make a local variable that is only accessible in the constructor)

p0 = new Point(x0, y0);
p1 = new Point(x1, y1);
p2 = new Point(x2, y2);
• 04-20-2011, 10:11 PM
Oconski
awesome it works! thanks for the help