代码之家  ›  专栏  ›  技术社区  ›  José Filipe Silva

使用用户介绍的参数生成人员数组

  •  1
  • José Filipe Silva  · 技术社区  · 2 年前

    我被要求建立一个系统,在那里我会有一个带有一系列功能的菜单,但最重要的是,这是关于安排医生和护士的预约。首先,我想,因为我们可能有很多不同的人(生病并想预约的普通人,医生和护士),我应该创建一个“人”类,并能够创建一组具有一组参数的人。在去的路上,我发现自己遇到了一个错误,说我的数组为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,即添加人员,然后他开始询问我在类中包含的参数,但首先,出于某种原因,它直接跳过了名称,然后他给了我这个错误,告诉我我的数组为空。

    编辑:按照建议翻译程序,希望它更容易。谢谢

    2 回复  |  直到 2 年前
        1
  •  2
  •   Grinding For Reputation    2 年前

    问题的原因是您没有创建的新实例 Pessoa 。因此,为了解决此问题,请使用以下代码

    people[0] = new Person(); //Make sure to pass parameters if they exist
    people[0].setPerson(nId,name,nTel);
    

    避免将数组用于这样的程序 而是考虑使用 ArrayList s

        2
  •  0
  •   Pantastix    2 年前

    您正在尝试读取/写入一个空的数组字段。然而,这个领域目前还不存在。要执行此操作,必须首先在此字段中保存一个对象。这可以通过以下方式实现。

    people[id] = new Person();
    perople[id].setPerson(nId, name, nTel)
    

    在这一点上,我建议为Person对象创建一个构造函数,它直接获取nId、name和nTel的值,并为对象设置这些值。 此外,这种用例实际上更有可能在ArrayList中实现,因为它是动态可扩展的,并且没有固定的大小。

    例如:

    List<Person> people = new ArrayList<Person>
    
    //now you can just add a new Person like this
    Person newPerson = new Person();
    newPerson.setPerson(nId, name, nTel);
    perople.add(newPerson);
    
    //and you can read a Person by its Index in the List. (kinda like in a Array)