我已經足夠新的Java編碼和新的堆棧交換。我一直試圖做這個汽車經銷商計劃作爲實踐。當我試圖將一輛汽車或一輛自行車添加到汽車的數組列表時,我總是收到一個空指針錯誤,我似乎無法弄清楚爲什麼,我幾乎認爲我的東西混在一起,在錯誤的地方,但我無法弄清楚哪裏去哪。我使用的是駕駛員,車輛超級車和汽車和自行車作爲從車輛繼承的兩個子類。 道歉,如果我不清楚任何事情,任何幫助表示讚賞。謝謝!不明白爲什麼我得到一個空指針錯誤
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();
}
}
添加你的堆棧跟蹤 –