Basically, I'm writing a program with a couple of different classes, one of them is called Doctor and it takes the data from another class (inheritance). On my main class, I have been trying to read a command from a file that has been given to us. then, assign the appropriate patient to the doctor based on the data on the file. I just keep running into NullPointerException right on line 71 (i already have the array HPersons filled).
Part of my code:
int DocID= in.nextInt();
int PatID= in.nextInt();
Doctor testDoc;
Patient testPat = null;
testDoc=null;
for(i=0; i<HPersons.length; i++){ **//error here**
if(HPersons[i].getID()==DocID){
if(HPersons[i] instanceof Doctor){
testDoc= (Doctor)HPersons[i]; //casting person to doc
break;
}
}
}
for(j=0; j<HPersons.length; j++){
if(PatID==HPersons[j].getID()){
if(HPersons[j] instanceof Patient){
testPat= (Patient)HPersons[j]; //casting person to patient
break;
}
}
}
if(testDoc==null){
outWrite.println("Command Assign_Doctor_Patient:");
outWrite.println("Doctor "+ DocID +" NOT FOUND ");
outWrite.println("-------------------------------------------------------------------------------");
}
if(testPat==null){
outWrite.println("Command Assign_Doctor_Patient:");
outWrite.println("Patient "+ PatID +" NOT FOUND ");
outWrite.println("-------------------------------------------------------------------------------");
}
// if the values match perfectly execute this
// AKA assigns doc to patient
testPat.setDoctor(testDoc); **//yellow line here**
// print these into the ouput file after assigning doc to patient
outWrite.println("Command Assign_Doctor_Patient:");
outWrite.println("Successfully Processed by the System:");
outWrite.println("Following are the details:");
outWrite.println(" Patient: "+ testPat.getName());
outWrite.println(" Assigned to Doctor: "+ testDoc.getName()); **//yellow line here**
outWrite.println("-------------------------------------------------------------------------------");
}