I was asked to build a system where I’ll have a menu with a bunch of functionalities, but the main point is that it is about scheduling doctor and nurse appointments. To start off, I thought, as we may have a lot of different people (normal people who are sick and want to schedule an appointment, doctors and nurses), I should create a "Person" class, and be able to create a bunch of people with a set of parameters. On my way to it, I find myself with an error that says that my array is null, even though the user is supposed to insert the information.
Here’s what I have in my Menu class so far. The part I’m trying to accomplish right now is just the number 1 on the menu, which means add a person, which I want to store in memory afterwards, and program the option 2 which is show people that are stored in memory.
import java.text.BreakIterator;
import java.util.Scanner;
public class Menu {
Person people[] = new Person[9];
public Menu(){
}
void showMenu(){
Scanner r = new Scanner(System.in);
int option;
System.out.println("==== Managing medical appointments ====");
System.out.println("1 - Add a person");
System.out.println("2 - Show people");
System.out.println("2 - Book an appointment");
System.out.println("3 - De-select an appointment");
System.out.println("4 - Show all appointments");
System.out.println("5 - Show appointments by date");
System.out.println("6 - Average enquiry time");
System.out.println("7 - Average time of enquiries by date");
System.out.println("8 - Save queries");
System.out.println("9 - Read queries");
System.out.println("0 - Exit");
System.out.println("Choose the desired option: ");
option = r.nextInt();
switch(option){
case 1: addPerson();
break;
case 2: showPeople();
break;
}
}
public void showPeople(){
for (int i =0 ; i< people.length ; i++){
System.out.println(people[i]);
}
}
public void addPerson() {
Scanner r = new Scanner(System.in);
int nId;
String name = "nothing";
int nTel;
System.out.println("Enter ID number: ");
nId = r.nextInt();
System.out.println("Enter the person's name: ");
name = r.nextLine();
System.out.println("Enter phone number: ");
nTel = r.nextInt();
people[0].setPerson(nId,name,nTel);
}
}
And here’s what is on my main. I have a whole lot of other classes, like establishment, which will extend to Hospital or Health center, Appointment, and so on. In this case on main, all I’ve done is create a Menu type of object, and call the showMenu function I’ve made.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Menu menu = new Menu();
menu.showMenu();
}
}
The error I get when I run my main:
==== Medical appointment management ====
1 - Add a person
2 - Show people
2 - Book an appointment
3 - Cancel an appointment
4 - Show all appointments
5 - Show appointments by date
6 - Average appointment time
7 - Average appointment time by date
8 - Save appointments
9 - Read appointments
0 - Exit
Choose the desired option:
1
Enter the identification number:
12313123
Enter the person's name:
Enter the phone number:
9712312
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Person.setPerson(int, String, int)" because "this.persons[0]" is null
at Menu.addPerson(Menu.java:49)
at Menu.showMenu(Menu.java:27)
at Main.main(Main.java:6)
Process finished with exit code 1
As you can see, it shows the menu, I go for option 1, which is add person, and then he starts asking for the parameters I’ve included in the class, but first off, for some reason it just skips the name right off, and then he gives me that error, telling me that my array is null.
EDITED : translated the program as suggested, hopefully it’s easier. Thanks!
>Solution :
The issue is caused because you are not creating a new instance of Pessoa. So in order to fix this use the following code
people[0] = new Person(); //Make sure to pass parameters if they exist
people[0].setPerson(nId,name,nTel);
Avoid using arrays for a program like this instead consider use ArrayLists