Problem with JavaNullException

0

I'm doing the following problem:

  

Create a record called Point that contains 2 fields: X (Real) and Y   (Real), which represent the coordinates of a point. Create a   Register called Triangle that contains 3 fields of type Point.   I develop the method "void typeTriangulo (Triangle T)" that given a   This triangle prints the type of triangle.

I have done this:

    class punto {

    static int x;
    static int y;
}

class triangulo {

    static punto a;
    static punto b;
    static punto c;
}

public class Ejercicio {


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        punto c1 = null;
        System.out.println("Ingrese la variable x");
        c1.x = sc.nextInt();
        sc.nextLine();
        System.out.println("Ingrese la variable y");
        c1.y = sc.nextInt();
        sc.nextLine();
        punto c2 = null;
        System.out.println("Ingrese la variable x");
        c2.x = sc.nextInt();
        sc.nextLine();
        System.out.println("Ingrese la variable y");
        c2.y = sc.nextInt();
        sc.nextLine();
        punto c3 = null;

        System.out.println("Ingrese la variable x");
        c3.x = sc.nextInt();
        sc.nextLine();
        System.out.println("Ingrese la variable y");
        c3.y = sc.nextInt();
        sc.nextLine();
        triangulo PrimerPuntoTriangulo = null;
        PrimerPuntoTriangulo.a.x = (c1.x);
        PrimerPuntoTriangulo.b.y = (c2.y);
        triangulo SegundoPuntoTriangulo = null;
        SegundoPuntoTriangulo.a.x = (c2.x);
        SegundoPuntoTriangulo.b.y = (c2.y);
        triangulo TercerPuntoTriangulo = null;
        TercerPuntoTriangulo.a.x = (c3.x);
        TercerPuntoTriangulo.b.y = (c3.y);

        if (PrimerPuntoTriangulo.equals(SegundoPuntoTriangulo) && SegundoPuntoTriangulo.equals(TercerPuntoTriangulo)) {
            System.out.println("El triangulo es Equilatero");

        } else if (PrimerPuntoTriangulo.equals(SegundoPuntoTriangulo) || PrimerPuntoTriangulo.equals(TercerPuntoTriangulo) || SegundoPuntoTriangulo.equals(TercerPuntoTriangulo)) {
            System.out.println("El triangulo es Isoceles");
        } else {
            System.out.println("El triangulo es Escaleno");

        }

    }
}

In this line:

if (PrimerPuntoTriangulo.equals(SegundoPuntoTriangulo) && SegundoPuntoTriangulo.equals(TercerPuntoTriangulo)) {

Netbeans throws me the following error:

  

java.lang.NullPointerException

Could someone help me with this?

    
asked by Bvss12 11.01.2018 в 21:54
source

2 answers

4

The problem happens at this point as you comment:

if (PrimerPuntoTriangulo.equals(SegundoPuntoTriangulo) && SegundoPuntoTriangulo.equals(TercerPuntoTriangulo)) {

The problem is that the 3 variables have a null value, therefore you can not call the equals() method in a variable with null value, so the error NullPointerException ,

you must initialize the variables:

   triangulo PrimerPuntoTriangulo = new triangulo();
   ...
   triangulo SegundoPuntoTriangulo = new triangulo();
   ...
   triangulo TercerPuntoTriangulo = new triangulo();

the same for the variables:

 punto c1 = new punto();
 ...
 punto c2 = new punto();
 ...
 punto c3 = new punto();
 ...

Update: Actually in this exercise you only need 1 triangle and its 3 points, plus you do not need to compare objects if you do not compare the properties of the objects, in this case the points. Also, avoid defining variables as static since when instantiating and defining a value this will remain in all classes.

This would be an example of what you need:

class punto {
    int x;
    int y;
}

class triangulo {
     punto a;
     punto b;
     punto c;
}

public class Ejercicio {


 public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        punto c1 = new punto();
        System.out.println("Punto1, Ingrese la variable x");
        c1.x = sc.nextInt();
        sc.nextLine();
        System.out.println("Punto1, Ingrese la variable y");
        c1.y = sc.nextInt();
        sc.nextLine();
        punto c2 = new punto();
        System.out.println("Punto2, Ingrese la variable x");
        c2.x = sc.nextInt();
        sc.nextLine();
        System.out.println("Punto2, Ingrese la variable y");
        c2.y = sc.nextInt();
        sc.nextLine();
        punto c3 = new punto();
        System.out.println("Punto3, Ingrese la variable x");
        c3.x = sc.nextInt();
        sc.nextLine();
        System.out.println("Punto3, Ingrese la variable y");
        c3.y = sc.nextInt();
        sc.nextLine();        

        if (((c1.x == c2.x) && (c1.y == c2.y) && (c2.x == c3.x) && (c2.y == c3.y))){
           System.out.println("El triangulo es Equilatero");
        }else if (((c1.x == c2.x) && (c1.y == c2.y) || (c1.x == c3.x) && (c1.y == c3.y) || (c2.x == c3.x) && (c2.y == c3.y))){
            System.out.println("El triangulo es Isoceles");
        } else {
           System.out.println("El triangulo es Escaleno");
        }

     }

}
    
answered by 11.01.2018 / 22:18
source
1

When creating the new instance of the objects, do not declare them null: punto c1 = new punto(); equal in triangle triangle PrimerPuntoTriangulo= new triangulo();

You have poor validation of the objects, you are validating if the objects are the same, they will not be the same, you should validate by the points in which you set the information in each object.

    
answered by 11.01.2018 в 22:19