I have to iterate over 3 triangles (Dreieck x,y,z, IT IS NECESSARY THAT THEY HAVE THESE NAMES), and I want them in one Array.
I have written a method "check If Valid" which checks if it is possible to construct these triangle. If it is possible, it should return "true". I have written a foreach loop and I want that for every triangle in the array dreiecke it should print "true" if it is possible to construct it and "false" if it isn't possible. In my case it throws a NullPointerException
.
Is there something wrong with initializing the Array?
public class DreieckTest {
public static void main(String[] args) {
Dreieck[] dreiecke = new Dreieck[3];
//triangle
Dreieck x = new Dreieck(1, 7, 5);
Dreieck y = new Dreieck(3, 4, 5);
Dreieck z = new Dreieck(5, 3, 3);
for(Dreieck dreieck: dreiecke) {
System.out.println(dreieck.istGültig());
}
}
}
and here is the class Dreieck:
public class Dreieck {
// attribute
private int a;
private int b;
private int c;
public Dreieck(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
//check if possible
public boolean istGültig() {
if (a + b > c ^ a + c > b ^ b + c > a) {
return true;
} else {
return false;
}
}