我被要求建立一个系统,在那里我会有一个带有一系列功能的菜单,但最重要的是,这是关于安排医生和护士的预约。首先,我想,因为我们可能有很多不同的人(生病并想预约的普通人,医生和护士),我应该创建一个“人”类,并能够创建一组具有一组参数的人。在去的路上,我发现自己遇到了一个错误,说我的数组为null,尽管用户应该插入信息。
到目前为止,这是我在菜单课上的内容。我现在试图完成的部分只是菜单上的数字1,这意味着添加一个人,我想稍后将其存储在内存中,并编程选项2,显示存储在内存的人。
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);
}
}
这是我的主菜。我还有很多其他的类,比如建立类,它将扩展到医院或健康中心、预约等等。在这种情况下,我所做的主要是创建一个Menu类型的对象,并调用我所创建的showMenu函数。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Menu menu = new Menu();
menu.showMenu();
}
}
运行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
正如你所看到的,它显示了菜单,我选择了选项1,即添加人员,然后他开始询问我在类中包含的参数,但首先,出于某种原因,它直接跳过了名称,然后他给了我这个错误,告诉我我的数组为空。
编辑:按照建议翻译程序,希望它更容易。谢谢