在数组中输入数据并显示null时出现程序错误

我在这里引用了一些修正,但仍然完全满足我的程序,但仍然没有完成。
问题1 =我的displayReg()出现“nullnullnullnull …”,我应该使用arraylist吗?
问题2 =我需要选择通过IC和NAME搜索searchReg()我该怎么做?
问题3 =需要在.txt文件上显示displayReg。

import java.util.*; public class RegisterMenu { private Driver[] newOwner; private final int MAX_ITEMS = 10; private int size = 0; public Driver newReg(){ Driver owner = new Driver(); Scanner scan = new Scanner(System.in); owner.setRegNo(size+1); System.out.print("Enter Name: "); owner.setName(scan.nextLine()); System.out.print("Enter IC: "); owner.setIc(scan.nextLine()); System.out.print("Enter PlateNo: "); owner.carInfo.setPlateNum(scan.nextLine()); System.out.print("Enter Color: "); owner.carInfo.setColor(scan.nextLine()); System.out.print("Enter Year: "); owner.carInfo.setYear(scan.nextLine()); System.out.print("Enter Make: "); owner.carInfo.setMake(scan.nextLine()); System.out.print("Enter Model: "); owner.carInfo.setModel(scan.nextLine()); System.out.print("Enter Capacity: "); owner.carInfo.setCapacity(scan.nextLine()); return owner; } public Driver editReg(){ Scanner scan = new Scanner(System.in); System.out.print("Enter RegNo to be edit: "); int input = scan.nextInt(); Driver owner = newReg(); newOwner[input] = owner; return owner; } public Driver searchReg(){ } public void displayReg(){ for(int i = 0; i < newOwner.length; i++){ newOwner[i].toString(); } } public RegisterMenu(){ newOwner = new Driver[MAX_ITEMS]; Scanner scan = new Scanner(System.in); System.out.println("1. Register New Car"); System.out.println("2. Edit Car Information"); System.out.println("3. Search Car Information"); System.out.println("4. Display Car List"); System.out.println("5. Exit"); System.out.print("Enter Selection: "); int s = scan.nextInt(); switch(s){ case 1: System.out.println("--Register New Car--"); if (size < MAX_ITEMS) { Driver owner = newReg(); newOwner[size++] = owner; } break; case 2: System.out.println("--Edit Car Infomation--"); editReg(); break; case 3: System.out.println("--Search Car Infomation--"); break; case 4: System.out.println("--Display Car Infomation--"); displayReg(); break; case 5: System.exit(0); default: System.out.println("Error selection"); } } public static void main (String args[]){ while(true){ RegisterMenu owner = new RegisterMenu(); } } } 

这是我的汽车课

 public class Car { public String plateNum; public String make; public String model; public String color; public String year; public String capacity; public Car(){ } public Car(String plateNum, String color, String year, String make, String model, String capacity){ this.plateNum = plateNum; this.color = color; this.year = year; this.make = make; this.model = model; this.capacity = capacity; } public String getPlateNum(){ return plateNum; } public String getMake(){ return make; } public String getModel(){ return model; } public String getColor(){ return color; } public String getYear(){ return year; } public String getCapacity(){ return capacity; } public void setPlateNum(String plateNum){ this.plateNum = plateNum; } public void setMake(String make){ this.make = make; } public void setModel(String model){ this.model = model; } public void setColor(String color){ this.color = color; } public void setYear(String year){ this.year = year; } public void setCapacity(String capacity){ this.capacity = capacity; } } 

这是我的Driver类

 public class Driver { private int regNo; private String name; private String ic; Car carInfo = new Car(); public Driver(){ } public Driver(int regNo, String name, String ic, Car carInfo){ this.regNo = regNo; this.name = name; this.ic = ic; this.carInfo = carInfo; } public int getRegNo(){ return regNo; } public String getName(){ return name; } public String getIc(){ return ic; } public void setRegNo(int regNo){ this.regNo = regNo; } public void setName(String name){ this.name = name; } public void setIc(String ic){ this.ic = ic; } public String toString(){ return "RegNo: "+getRegNo()+"\tName: "+getName()+"\tIc: "+getIc()+ "\tPlateNo: "+carInfo.getPlateNum()+"\tColor: "+carInfo.getColor()+"\tYear: "+carInfo.getYear()+ "\tMake: "+carInfo.getMake()+"\tModel: "+carInfo.getModel()+"\tCapacity: "+carInfo.getCapacity(); } } 

Register[] a = new Register[i]; - Register[] a = new Register[i]; -创建一个包含0个项目的数组。

a[i].Reg(); – 访问没有任何项目的数组的位置0。

这是一个教程

 int i = 0; Register[] a = new Register[i]; 

您实际上是在尝试创建0大小的数组。 但是当你尝试在i = 0时访问[i]时,它会尝试访问数组中第1个位置的元素(数组有0作为第一个位置)。

你也把它放在一个while循环中,这意味着你在每个循环周期创建一个0大小的新数组。

尝试把i = 10(或者你的程序逻辑说的任何东西)并将数组创建放在循环之外