我对Java编码和新的堆栈交换都足够新。我一直在努力做这个汽车经销商计划作为实践。当我试图将汽车或自行车添加到车辆的arraylist时,我一直得到一个空指针错误,我似乎无法弄清楚为什么,我几乎认为我把事情搞混了,在错误的地方,但我无法弄清楚去哪里。我使用司机,车辆超级舱和汽车和自行车作为继承车辆的两个子类。 抱歉,如果我不清楚任何事情,任何帮助表示赞赏。谢谢!
public class BMWdriver {
ArrayList<Vehicles> list;
public void Driver() {
list = new ArrayList<Vehicles>();
}
public void inputCarDetails() {
Scanner scan = new Scanner(System.in);
String model, colour, fuelType, layout, frame, vehicleType;
int doors, stock, displacement, topSpeed, stroke, noSeats, noVehicles;
double price, fuelMpg;
boolean sunroof;
Vehicles car;
System.out.println("----Entering car details----");
System.out.println("\nEnter model");
model = scan.nextLine();
System.out.println("Enter Price");
price = scan.nextDouble();
scan.nextLine();
System.out.println("Enter colour");
colour = scan.nextLine();
System.out.println("Enter no. in stock");
stock = scan.nextInt();
System.out.println("Enter MPG");
fuelMpg = scan.nextDouble();
System.out.println("Enter displacement");
displacement = scan.nextInt();
System.out.println("Enter top speed");
topSpeed = scan.nextInt();
System.out.println("Enter no. of doors");
doors = scan.nextInt();
System.out.println("Enter fuel type");
fuelType = scan.nextLine();
System.out.println("Enter wheel layout");
layout = scan.nextLine();
System.out.println("Enter sunroof (true/false)");
sunroof = scan.nextBoolean();
car = new Cars(model, price, colour, stock, fuelMpg, displacement, topSpeed, doors, fuelType, layout, sunroof);
list.add(car);
}
public void inputBikeDetails() {
Scanner scan = new Scanner(System.in);
String model, colour, fuelType, layout, frame, vehicleType;
int doors, stock, displacement, topSpeed, stroke, noSeats, noVehicles;
double price, fuelMpg;
boolean sunroof;
Vehicles bike;
System.out.println("----Entering bike details----");
System.out.println("\nEnter model");
model = scan.nextLine();
System.out.println("Enter Price");
price = scan.nextDouble();
scan.nextLine();
System.out.println("Enter colour");
colour = scan.nextLine();
System.out.println("Enter no. in stock");
stock = scan.nextInt();
System.out.println("Enter MPG");
fuelMpg = scan.nextDouble();
System.out.println("Enter displacement");
displacement = scan.nextInt();
System.out.println("Enter top speed");
topSpeed = scan.nextInt();
System.out.println("Enter engine stroke");
stroke = scan.nextInt();
System.out.println("Enter no. of seats");
noSeats = scan.nextInt();
System.out.print("Enter the frame type");
frame = scan.nextLine();
bike = new Bikes(model, price, colour, stock, fuelMpg, displacement, topSpeed, stroke, noSeats, frame);
list.add(bike);
}
public static void main(String[] args) // main method
{
BMWdriver driver = new BMWdriver();
driver.startMenu();
driver.inputCarDetails();
driver.inputBikeDetails();
}
}
答案 0 :(得分:2)
ArrayList<Vehicles> list;
你没有调用Driver方法初始化arraylist
public static void main (String[] args) // main method
{
BMWdriver driver = new BMWdriver();
driver.Driver();
driver.startMenu();
driver.inputCarDetails();
driver.inputBikeDetails();
}
}
您正尝试将数据添加到尚未初始化的列表中。 你应该确保在添加任何数据之前初始化它。
答案 1 :(得分:0)
你错误拼写了构造函数
public void Driver()
这应该是
public BMWdriver()
然后您的list
属性将被初始化,添加到列表时的NPE将会消失。