Results 1 to 5 of 5
Thread: Triangle class help
 04202011, 08:50 PM #1Member
 Join Date
 Apr 2011
 Posts
 3
 Rep Power
 0
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);
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()); } }
 04202011, 09:07 PM #2Member
 Join Date
 Dec 2009
 Posts
 36
 Rep Power
 0
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);
 04202011, 09:23 PM #3Member
 Join Date
 Apr 2011
 Posts
 3
 Rep Power
 0
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()); } }
 04202011, 09:24 PM #4Member
 Join Date
 Dec 2009
 Posts
 36
 Rep Power
 0
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);
 04202011, 10:11 PM #5Member
 Join Date
 Apr 2011
 Posts
 3
 Rep Power
 0
Similar Threads

Pascals Triangle
By Bgreen7887 in forum New To JavaReplies: 6Last Post: 12162010, 12:22 AM 
triangle
By Shyamz1 in forum New To JavaReplies: 4Last Post: 11072010, 06:12 PM 
Outputting a * triangle
By blackbeltsas in forum New To JavaReplies: 1Last Post: 10172010, 11:39 AM 
Triangle
By jkswebsite in forum New To JavaReplies: 8Last Post: 01102009, 02:08 PM 
Pascal Triangle help
By Magic101 in forum New To JavaReplies: 4Last Post: 05012008, 07:51 PM
Bookmarks