Java, I can not find the error. (basic OOP) [closed]

-2
package nueve;

import java.util.Scanner;

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

    public void cargardatos() {
        System.out.println("Ingrese la cantidad de Alumnos: ");
        arreglo=sc.nextInt();
        for (int i=0; i<arreglo; i++ ){

            System.out.println("Ingrese nombre de alumno: ");
            Nombres [i] = sc.nextLine();

            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();
    }

}
    
asked by Ibarra Emiliano 30.05.2017 в 16:07
source

2 answers

1

As I see in the class nueve you use the arrays:

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

But these do not initialize them anywhere, you would miss, for example:

Nombres = new String[arreglo];
Legajo = new int[arreglo];
AñoNacimiento = new int[arreglo];

This would go right after reading the fix value.

    
answered by 30.05.2017 в 16:47
0

Your upload data function would go like this:

public void cargardatos() {
    System.out.println("Ingrese la cantidad de Alumnos: ");
    arreglo=sc.nextInt();

    Nombres = new String[arreglo];
    Legajo = new int[arreglo];
    AñoNacimiento = new int[arreglo];
    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();
    }
}

Initializing your arrays so that you do not mark the java.lang.NullPointerException . Just modify the following line of code: Nombres[i] = sc.nextLine(); per Nombres[i] = sc.next(); since the line was skipped and you could not write the name.

    
answered by 30.05.2017 в 16:59