I'm trying to add data to each item in an array of objects from Scanner input. But I am getting a null pointer exception at patients[i].setPatientId(id)
.
My code is as follows:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Patient[] patients = new Patient[3];
for (int i = 0; i < patients.length; i++) {
System.out.println("Enter a patient Id: ");
int id = input.nextInt();
System.out.println("Enter the patient name: ");
String name = input.next();
System.out.println("Enter Amount: ");
double amount = input.nextDouble();
patients[i].setPatientId(id);
patients[i].setPatientName(name);
patients[i].setAmount(amount);
}
}
Can someone please tell me what I am doing wrong ?