Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
public boolean equals(Triangle t) {
        if (this.a.getDistance(this.b) == t.a.getDistance(t.b)
                && this.a.getDistance(this.c) == t.a.getDistance(t.c)
                && this.b.getDistance(this.c) == t.b.getDistance(t.c))
                return true;
               return false;
    }


C#
public boolean isTriangle() {
    if (this.a.getDistance(this.b) < (this.a.getDistance(this.c) + this.b.getDistance(this.c))
            && this.a.getDistance(this.b) > (this.a.getDistance(this.c) - this.b.getDistance(this.c))
            && this.a.getDistance(this.c) < (this.a.getDistance(this.b) + this.b.getDistance(this.c))
            && this.a.getDistance(this.c) > (this.a.getDistance(this.b) - this.b.getDistance(this.c))
            && this.b.getDistance(this.c) < (this.a.getDistance(this.c) + this.a.getDistance(this.b))
            && this.b.getDistance(this.c) > (this.a.getDistance(this.c) - this.a.getDistance(this.b))) {
        return true;
    }
    return false;
}


What I have tried:

I wrote 2classes as Point and Triangle and 2functions above :one of them is equals that i want to check that "do these two triangles equal? " and another function is isTringle that i want to check that can these points be points of triangle like can this points create triangle?


and when i want to run my program i get error like this:
C#
Exception in thread "main" java.lang.NullPointerException
	at javaapplication35.Triangle.equals(Triangle.java:54)
	at javaapplication35.JavaApplication35.main(JavaApplication35.java:34)
C:\Users\User\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)


when i click adresses which errors show me,i go to above equals function.
what is wrong with this function?can anyone help me?
thank you in advance :)


C#
public class JavaApplication35 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Point p1 = new Point(6, 8);
        Point p2 = new Point(3, 4);
        Point p3 = new Point(3, 4);
        /*System.out.println(p1.getDistance(p2));
        System.out.println(p3.getDistance(p2));
        System.out.println(p1.toString());
        System.out.println(p2.toString());
        System.out.println(p3.toString());
        System.out.println(p1.isSame(p2));
        System.out.println(p3.isSame(p2));*/
        Triangle t1 = new Triangle();
        t1.setA(p1);
        t1.setB(p2);
        t1.setC(p3);
        System.out.println((t1.getPerim()));
        Triangle t2 = new Triangle();
        t2.setA(p1);
        t2.setB(p2);
        t2.setC(p3);
        System.out.println(t1.equals(t2));
        System.out.println(t1.isTriangle());
    }
}
Posted
Updated 6-Nov-16 3:26am
v4

1 solution

t2 never initialized, so it's points are null (a, b, c)...
Java
if ((this.a.getDistance(this.b) == t.a // <= this is null!!!
 
Share this answer
 
Comments
Member 12702056 6-Nov-16 8:40am    
you mean for knowing" do they equal or not " i must give the points to t2 in main function?
for example,
after t2 i must set points?:
Triangle t2 = new Triangle();
t2.setA(p1);
t2.setB(p2);
t2.setC(p3);
System.out.println(t1.equals(t2));
System.out.println(t1.isTriangle());
Kornfeld Eliyahu Peter 6-Nov-16 8:44am    
If t2 has no points, you actually didn't defined any triangle! so what you want to compare???
Member 12702056 6-Nov-16 8:42am    
could i get what you mean?
Member 12702056 6-Nov-16 9:04am    
thank you very much
and one more thing
i corrected my code above:
public boolean equals(Triangle t) {
if (this.a.getDistance(this.b) == t.a.getDistance(t.b)
&& this.a.getDistance(this.c) == t.a.getDistance(t.c)
&& this.b.getDistance(this.c) == t.b.getDistance(t.c))
return true;
return false;
}
it must be like this,yes?
i must compare these points with points of Triangle t
yeah?
Kornfeld Eliyahu Peter 6-Nov-16 9:08am    
As I see you totally missed my point... I do not talking about your comparison (triangles can be defined as equals in more than one way), but about the fact that you try to compare an uninitialized triangle (t2) in your code...
No matter how you compare it as long as there are no points definded...
t.a, t.b and t.c still will be null...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900