Results 1 to 5 of 5
  1. #1
    Oconski is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default 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:

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

    Java 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());
       }
    }
    Java 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

  2. #2
    jojo is offline Member
    Join Date
    Dec 2009
    Posts
    36
    Rep Power
    0

    Default

    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

    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

  3. #3
    Oconski is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    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:

    Java 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());
       }
    }

  4. #4
    jojo is offline Member
    Join Date
    Dec 2009
    Posts
    36
    Rep Power
    0

    Default

    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);

  5. #5
    Oconski is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Pascals Triangle
    By Bgreen7887 in forum New To Java
    Replies: 6
    Last Post: 12-16-2010, 01:22 AM
  2. triangle
    By Shyamz1 in forum New To Java
    Replies: 4
    Last Post: 11-07-2010, 07:12 PM
  3. Outputting a * triangle
    By blackbeltsas in forum New To Java
    Replies: 1
    Last Post: 10-17-2010, 12:39 PM
  4. Triangle
    By jkswebsite in forum New To Java
    Replies: 8
    Last Post: 01-10-2009, 03:08 PM
  5. Pascal Triangle help
    By Magic101 in forum New To Java
    Replies: 4
    Last Post: 05-01-2008, 08:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •