Ich versuche, etwas Java-Code schreiben, aber ich bekomme eine Ausnahme. mein Problem ist, dass ich einen Null-Zeiger Ausnahme bin immer wenn ich versuche, Athlet das Programm hinzuzufügen ist Athleten zu übernehmen und die calculate Note Durchschnitt Hier ist mein CodeArraylist druckt kein erstes Element
public class AthleteTest {
final int MAX_ATHELETE = 200;
private int count=0;
Athlete[] at = new Athlete[MAX_ATHELETE];
Scanner sc = new Scanner(System.in);
public void addAthletes(){
char add = 'Y';
while(add == 'Y'){
System.out.println("name:");
String name = sc.nextLine();
at[count].setName(name);
//Get athlete's Id number
System.out.println("id :");
int id = sc.nextInt();
at.setId(id);
//sc.nextLine();
count++;
System.out.println("Would you like to add another athlete? Y/N");
add = Character.toUpperCase(sc.next().charAt(0));
sc.nextLine();
}
}
}
my Athlete class is as follow
public class Athlete {
private String name;
private int id;
private double [] grades;
public Athlete(){
this.name = null;
this.id= 0;
}
public Student(String name, int id){
this.name = name;
this.id= id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Wo ist ArrayList? –
'bei [count] .setName (name)' - ist 'bei [count]' initialisiert? Wenn nicht, erhalten Sie 'NullPointerException'. Sie sollten 'at [count] = new Athlet()' zuerst –
Ja, tat ich, aber es gibt immer noch eine Nullzeiger Ausnahme –