-1

I'm working on a lab for programming principles II, and I have a class that makes a point, with methods for setting the point, and calculating the distance between other points. Calculating the distance worked fine when testing it with a runner for just the class, but when I make other classes that use it as an object, I'm getting an error with the distance formula.

import java.lang.Math;

public class MyPoint {
private double x;
private double y;

public MyPoint(double dubx, double duby)
{
    x=dubx;
    y=duby;
}

public void setX(double dub) {
    x = dub;
}

public void setY(double dub) {
    y = dub;
}

public double getX() {
    return x;
}

public double getY()
{
    return y;
}
public double distance (MyPoint otherPoint)
{
    return Math.sqrt(Math.pow((otherPoint.getX()-getX()),2)+(Math.pow((otherPoint.getY()-getY()),2)));
}
public MyPoint midpoint(MyPoint otherPoint)
{
    MyPoint point = new MyPoint((otherPoint.getX()+getX()/2),(otherPoint.getY()+getY())/2);
    return point;
}
}

That's the class I'm getting the error on. The distance part is getting a null pointer exception.

Here's what I'm passing in:

import java.lang.Math;

public class MyTriangle
{
private MyPoint v1;
private MyPoint v2;
private MyPoint v3;

public MyPoint getPoint1()
{
    return v1;
}
public MyPoint getPoint2()
{
    return v2;
}
public MyPoint getPoint3()
{
    return v3;
}
public void setPoint1(double x, double y)
{
    v1= new MyPoint(x,y);
}
public void setPoint2(double x, double y)
{
    v2 = new MyPoint(x,y);
}
public void setPoint3(double x, double y)
{
    v2= new MyPoint(x,y);
}
public double getArea()
{
    double a= v2.distance(v3);
    double b= v1.distance(v3);
    double c= v1.distance(v2);
    double s= (a+b+c)/2;
    return Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
}

public class TestMyTriangle
{
public static void main(String [] args)
{
    MyTriangle tr1 = new MyTriangle();
    tr1.setPoint1(17,17);
    tr1.setPoint2(5,30);
    tr1.setPoint3(5,17);
    System.out.println("Area:\t"+tr1.getArea());
    }
}

And the error:

Exception in thread "main" java.lang.NullPointerException
at MyPoint.distance(MyPoint.java:34)
at MyTriangle.getArea(MyTriangle.java:37)
at TestMyTriangle.main(TestMyTriangle.java:9)

I can't seem to figure it out. Please help.

1 Answers1

-1

You get the Nullpointer because v3 is null:

fix with:

public void setPoint3(double x, double y)
{
    v3= new MyPoint(x,y); // instead of v2
}

Another tipp: to calculate a square dont use Math.pow(x,2). Altough it works. The code is cleaner and faster if you use

 x*x instead Math.pow(x,2);
AlexWien
  • 28,470
  • 6
  • 53
  • 83
  • You got me. Still have an error though. The area is supposed to be 30, but I got 78. – No longer in college Jan 20 '16 at 21:02
  • Try to accept my answer, since it is closed now, you cannot get another answer. – AlexWien Jan 20 '16 at 21:03
  • 1
    Why are you even answering duplicate questions in the first place? – user1231232141214124 Jan 20 '16 at 21:05
  • Nevermind, just calculated it, 78 is correct, lab prompt said it should be 30. guess the prof was wrong on that. Thanks. Sorry for wasting your time with a dumb coding error like that. – No longer in college Jan 20 '16 at 21:07
  • @redFIVE I cannot answer a duplicate question. It was not marked as duplicate when I answered it. I did not see the link to the duplicate question. (the link location recently changed at SO, it was not at the top as before. And the duplicate does not directly answer his question. and the hint in the comment was difficult to understand. And it is maximum ugly to use Math.pow(x,2) instead x*x – AlexWien Jan 20 '16 at 21:14
  • You need us to tell you a Null Pointer question is a duplicate? Its hand down the most often asked question. Don't feign ignorance because you want some easy rep... Regardless using `x*x` is bad style in my opinion. Math.pow(x,2) is pretty self explanitory. – user1231232141214124 Jan 20 '16 at 22:01
  • @redFIVE I don't think there's anything intrinsically wrong with using `x * x`, especially for beginners. What's the big issue with it in this case? – Dave Newton Jan 20 '16 at 23:10
  • @redFive seems to be not an experienced developper. x * x is the prefered solution, fo all, beginners and professionals. Sun always uses x * x for good reasons. Just look into Math.power and you see that is written for non integer powers, dealing with a huge number of special cases, befire even starting the calculation.. Using Math.pow(x,2) code gets unreadable. e.g dx * dx + dy * dy, is really well readable, while the Math.pow variant obfiuscates the formula. – AlexWien Jan 21 '16 at 16:05
  • Yes, I'm the non-experience developer answering duplicate null pointer questions hoping for easy imaginary internet points. You got me all figured out. – user1231232141214124 Jan 21 '16 at 16:15