-1

This is a part of my program's sample run that shows an error.

Please enter your the number of your selection : 5
Enter the blood Type : b
Exception in thread "main" java.lang.NullPointerException
at Hospital.getNumberOfPatientsBlood(Hospital.java:136)
at Hospital.main(Hospital.java:71)

I have this error, and I don't know what's wrong. It should give me a message that says there is a zero patient has a blood type of B.

This is the part that it shows the error:

The method:

   public static int getNumberOfPatientsBlood( String bloodT ){

  int bloodTypeCount=0;
  for (int j =0; j<hospitalPatient.length;j++){

     if (hospitalPatient[j].equals(bloodT))
        bloodTypeCount++;

     else

        continue; 

  }
  return -1 ;

   }

The other error is in the main:

        case 5:
           input.nextLine();              
           System.out.print("Enter the blood Type : ");
           String searchBloodType = input.nextLine();

           if (getNumberOfPatientsBlood(searchBloodType)!=-1)
              System.out.println("the total number of patients having the same Blood Type: "+getNumberOfPatientsBlood(searchBloodType));

           break;
Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
Shahad
  • 3
  • 5

1 Answers1

0

I guess hospitalPatient is null, initialize it first.

CalibeR.50
  • 370
  • 2
  • 13