-1

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("-------------------------------------------------------------------------------");
            
        }
Deem
  • 1
  • 1
  • If you get a NPE on line 71 there are only 2 possibilities that I see: 1. Either `HPersons[i]` is `null`, or 2. the `getID()` method is supposed to return an `Integer` and returns `null` that java then tries to autobox into a primitive `int` leading to a NPE. So check which one of those is the case for you – OH GOD SPIDERS May 11 '22 at 09:23
  • Your edit now says "//error here**" in what was line 70 in your screenshot. So which one is it? In line `for(i=0; i – OH GOD SPIDERS May 11 '22 at 09:29
  • line 70 yes when i copied the code i skipped a comment line – Deem May 11 '22 at 09:30
  • Well, then it's even more simple, the only possible cause for a NPE in `for(i=0; i – OH GOD SPIDERS May 11 '22 at 09:35

0 Answers0