Java Programming (Basic POO: Exception in thread "main" java.lang.NullPointerException)

1

I copy and paste (it seems to me more comfortable than an image) the code.

package nueve;
import java.util.Scanner;

public class nueve {
    Scanner sc = new Scanner (System.in);
    private int arreglo;
    private int AñoEdad=2017;
    private String AlumnoMayorEdad;
    private int Año1, Año2;
    String Nombres [] ;
    int Legajo [] ;
    int AñoNacimiento [];


    public nueve() {
        System.out.println("Ingrese la cantidad de Alumnos: ");
        arreglo=sc.nextInt();
        String Nombres [] = new String [arreglo];
        int Legajo [] = new int [arreglo];
        int AñoNacimiento [] = new int [arreglo];
    }


    public void cargardatos() {
        for (int i=0; i<arreglo; i++ ){         
            System.out.println("Ingrese nombre de alumno: ");
            Nombres [i] = sc.next();        
            System.out.println("Ingrese el legajo del alumno " + Nombres[i] + ": ");
            Legajo [i] = sc.nextInt();

            System.out.println("Ingrese año de nacimiento del alumno " + Nombres[i] + ": ");
            AñoNacimiento [i] = sc.nextInt();
        }
    }
    public void CualEsElDeMayorEdad(){
        for (int i=0; i<arreglo; i++){
            if (AñoNacimiento[i]<AñoEdad){
                AlumnoMayorEdad=Nombres[i];
                AñoEdad=AñoNacimiento[i];
            }
        }
    }   
    public String DimeEdad(){
        return "El alumno de mayor edad es: " + AlumnoMayorEdad;
    }
    public void EntreAños(){
        System.out.println("Ingrese los dos años, primero el mayor edad: ");
        Año1=sc.nextInt();
        Año2=sc.nextInt();
        for (int i=0; i<arreglo; i++){
            if (AñoNacimiento[i]> Año1 && AñoNacimiento[i]<Año2){
                System.out.println("El alumno " + Nombres[i] + " esta entre esos años");
            }
        }
    }
}

package nueve;

public class uso_nueve {

    public static void main(String[] args) {

        nueve desempeño = new nueve();

        desempeño.cargardatos();
        desempeño.CualEsElDeMayorEdad();
        desempeño.DimeEdad();
        desempeño.EntreAños();
    }
}

** THE PROBLEM IS WHEN I ENTER THE NAME OF THE FIRST STUDENT: Exception in thread "main" java.lang.NullPointerException. I STILL DO NOT HAVE EXCEPTION KNOWLEDGE. FORGIVENESS FOR IGNORANCE, SHOULD BE A PAVADA THAT I DO NOT REALIZE **

I HOPE THAT YOU CAN HELP ME, I LEAVE THE STATEMENT YOU LEFT ME: Load three vectors containing file, name of the student and year of birth. Make a query by name and determine which is the oldest student. Enter two dates (only the year) and show all the students who were born between those years.

    
asked by Ibarra Emiliano 31.05.2017 в 01:12
source

2 answers

1

The problem is in the initialization of the variables in the constructor, you are defining again the data type of each variable and it is not necessary because you defined them before, when you redefine them, those variables are only available within the constructor, and the global variables are never initialized, to solve you should only remove the data types of the variables.

Example:

public nueve() {
    System.out.println("Ingrese la cantidad de Alumnos: ");
    arreglo=sc.nextInt();
    Nombres = new String [arreglo];
    Legajo = new int [arreglo];
    AñoNacimiento = new int [arreglo];
}

That should be enough for the code to work.

Tip look at the conventions for writing Java code

    
answered by 31.05.2017 в 02:39
1

The problem is that you are trying to add elements to an array that is not initialized / sized,

Nombres [i] = sc.nextLine(); 

If you define the array in the constructor you will not be able to use it in your other methods of your program:

 public nueve() {
        System.out.println("Ingrese la cantidad de Alumnos: ");
        arreglo=sc.nextInt();
        String Nombres [] = new String [arreglo];
        int Legajo [] = new int [arreglo];
        int AñoNacimiento [] = new int [arreglo];
    }

The solution is to use class variables to define these arrays, to be able to use them in any method of your class:

//String Nombres [] ;
private String[] Nombres;
//int Legajo [] ;
private int[] Legajo;
//int AñoNacimiento [];
private int[] AñoNacimiento;

in the constructor then, the arrays that you can use without problems in your program will be initialized:

public nueve() {
    System.out.println("Ingrese la cantidad de Alumnos: ");
    arreglo=sc.nextInt();
    //String Nombres [] = new String [arreglo];
     Nombres = new String [arreglo];
    //int Legajo [] = new int [arreglo];
     Legajo = new int [arreglo];
    //int AñoNacimiento [] = new int [arreglo];
     AñoNacimiento= new int [arreglo];
}

This would be the code:

import java.util.Scanner;

public class nueve {


    public static void main(String[] args) {

        nueve desempeño = new nueve();

        desempeño.cargardatos();
        desempeño.CualEsElDeMayorEdad();
        desempeño.DimeEdad();
        desempeño.EntreAños();
    }


    Scanner sc = new Scanner (System.in);
    private int arreglo;
    private int AñoEdad=2017;
    private String AlumnoMayorEdad;
    private int Año1, Año2;
    private String[] Nombres;
    //int Legajo [] ;
    private int[] Legajo;
    //int AñoNacimiento [];
    private int[] AñoNacimiento;


    public nueve() {
        System.out.println("Ingrese la cantidad de Alumnos: ");
        arreglo=sc.nextInt();
        //String Nombres [] = new String [arreglo];
         Nombres = new String [arreglo];
        //int Legajo [] = new int [arreglo];
         Legajo = new int [arreglo];
        //int AñoNacimiento [] = new int [arreglo];
         AñoNacimiento= new int [arreglo];
    }


    public void cargardatos() {
        for (int i=0; i<arreglo; i++ ){         
            System.out.println("Ingrese nombre de alumno: ");
            Nombres [i] = sc.next();        
            System.out.println("Ingrese el legajo del alumno " + Nombres[i] + ": ");
            Legajo [i] = sc.nextInt();

            System.out.println("Ingrese año de nacimiento del alumno " + Nombres[i] + ": ");
            AñoNacimiento [i] = sc.nextInt();
        }
    }
    public void CualEsElDeMayorEdad(){
        for (int i=0; i<arreglo; i++){
            if (AñoNacimiento[i]<AñoEdad){
                AlumnoMayorEdad=Nombres[i];
                AñoEdad=AñoNacimiento[i];
            }
        }
    }   
    public String DimeEdad(){
        return "El alumno de mayor edad es: " + AlumnoMayorEdad;
    }
    public void EntreAños(){
        System.out.println("Ingrese los dos años, primero el mayor edad: ");
        Año1=sc.nextInt();
        Año2=sc.nextInt();
        for (int i=0; i<arreglo; i++){
            if (AñoNacimiento[i]> Año1 && AñoNacimiento[i]<Año2){
                System.out.println("El alumno " + Nombres[i] + " esta entre esos años");
            }
        }

    }


}
    
answered by 31.05.2017 в 06:51