Good I'm doing a program where I must (enter the name and the note of some students) make 2 arrays one of the String type and another of the float type. I have a problem in the main when I want to enter the student data by console. First I put the Scanner then I declare two arrays of the type String and float, but they tell me that I must initialize it and eclipse tells me that I should put them equal to null, but I get a warning in this part:
nombre[i] = teclado.next();
nota [i]= teclado.nextFloat();
that tells me that I can not put them null and when I do it and input the first data I get:
Exception in thread "main" java.lang.NullPointerException at arrays_alumnos.PruebaAlumno.main (PruebaAlumno.java:20)
I do not know if the problem is when I put the arrays on keyboard.next (); / teclado.nextFloat (); or why not initialize the variables and in doing so I put them null and it's wrong.
public class Alumnos {
private String nombreAlumno[] ;
private float nota[];
public Alumnos() {
this.nombreAlumno = new String [5] ;
this.nota = new float [5];
}
public Alumnos(String nombre[] , float[] nota) {
this.nombreAlumno = nombre ;
this.nota= nota;
}
public String[] getNombre() {
return this.nombreAlumno;
}
public void setNombre(String [] nombreAlumno ) {
this.nombreAlumno = nombreAlumno;
}
public float [] getNota() {
return this.nota;
}
public void setNota(float [] nota) {
this.nota = nota ;
}
public void verificacionDeNota() {
for (int i = 0; i < nombreAlumno.length; i++) {
if(nota[i]>=7.0) {
System.out.println("Alumno : " +nombreAlumno + " Promocionado.");
}
if (nota[i] >=1.0 && nota[i] <=3.0) {
System.out.println("Alumno : " +nombreAlumno +" Reprobado.");
}
if(nota[i]>=4 && nota[i]<=6) {
System.out.println("Alumno : " +nombreAlumno +" Aprobado.");
}
}
}
}
import java.util.Scanner;
public class TestStudent {
public static void main(String[] args) {
Scanner teclado = new Scanner (System.in);
String nombre[] = null ;
float nota[] = null;
for ( int i = 0; i < 5; i++) {
System.out.println((i+1)+".Ingrese nombre : ");
nombre[i] = teclado.next();
System.out.println((i+1)+".Ingrese nota : ");
nota [i]= teclado.nextFloat();
Alumnos curso1 = new Alumnos (nombre , nota);
}
}
}