So. I have this code.
It throws a NullPointerException that I cannot locate the source of.
Any help would be appreciated.
Note: console is a Scanner, studentlist is an ArrayList, both are global variables.
public static void newStudent(){
String cmd;
String[] name = new String[2];
boolean gotName = false;
int SID = 0;
boolean gotSID = false;
ArrayList<Integer> testScores = new ArrayList();
boolean gotTestScores = false;
boolean doContinue = true;
while(true){
if(!gotName){
System.out.println("Please input the student's name, first and last in that order. This system does not support middle names.");
name[0]=console.next();
System.out.println(name[0]);
name[1]=console.next();
System.out.println(name[1]);
gotName=true;
}
if(!gotSID){
gotSID = true;
System.out.println("Please input the student ID number. Do not prefix it with #.");
try{
cmd = console.next();
SID = Integer.parseInt(cmd);
System.out.println(SID);
}
catch(IllegalArgumentException | NullPointerException ex){
gotSID = false;
doContinue = false;
System.out.println("Please input a number, and only a number.");
}
}
if(!gotTestScores && doContinue){
gotTestScores = true;
System.out.println("Please input any test scores. Input anything that is not a number to finish inputting test scores or skip this input.");
while(true){
if(console.hasNextInt()){
cmd = console.next();
testScores.add(Integer.parseInt(cmd));
}
else break;
}
}
if(doContinue){
if(testScores.isEmpty())testScores.add(0);
studentlist.add(new Student(name[0],name[1],SID,testScores));
break;
}
doContinue = true;
}
}
The behavior of Scanner precludes name[] from being null (and I'm not giving it null input anyway), and testScores specifically has an if statement devoted to making sure it isn't actually null, but the exception is thrown whether I give the portion of the code responsible for testScores input or not. The NPE is thrown when I do studentlist.add(new Student(...))
So...What's actually null here, and how the heck am I supposed to fix it?
E: Stacktrace.
Exception in thread "main" java.lang.NullPointerException
at studentmanager.StudentManager.newStudent(StudentManager.java:104)
at studentmanager.StudentManager.main(StudentManager.java:26)
Java Result: 1