-1

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;
    }
}
ᗩИᎠЯƎᗩ
  • 2,122
  • 5
  • 29
  • 41
  • 1
    You never add your x,y and z Objects to the array you created. So the array will contain only null values. – OH GOD SPIDERS Oct 13 '17 at 12:00
  • also change ^ to && – mlecz Oct 13 '17 at 12:02
  • @mlecz Are you sure && is what he intends to do? "a + b > c && a + c > b && b + c > a" doesn't seem to be possible to be true. – OH GOD SPIDERS Oct 13 '17 at 12:03
  • @OHGODSPIDERS yeah, because it should be also >=. We are good at debugging together man. Maybe some pair programming? – mlecz Oct 13 '17 at 12:05
  • thanks! so it is better to add "private String name" and "String name" + this.name = name; in the constructor to declare the triangles "x", "y", and "z"? – Juri_Wolkow Oct 13 '17 at 12:18
  • See https://stackoverflow.com/a/9043523/1531124 first of all. And do some research before posting your question the next time. – GhostCat Oct 13 '17 at 12:20

1 Answers1

3

Use this

dreiecke[0]= new Dreieck(1, 7, 5);
dreiecke[1] = new Dreieck(3, 4, 5);
dreiecke[2] = new Dreieck(5, 3, 3);
Karan
  • 448
  • 4
  • 9
  • thank you so much!! – Juri_Wolkow Oct 13 '17 at 12:04
  • I still have to write a method "isGreaterThan" which compares the acreage area of one triangle (dreieck) with another. if it is greater it should return "true". Can you help me with that as well? – Juri_Wolkow Oct 13 '17 at 12:09
  • 1
    This is not a "more unrelated questions via comments" community. Why dont you try to do your homework yourself first, seriously? What do you learn from asking others to do you homework for you? – GhostCat Oct 13 '17 at 12:21