2016-04-27 166 views
0

我已經足夠新的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(); 
} 
} 
+0

添加你的堆棧跟蹤 –

回答

2
ArrayList<Vehicles> list; 

你是不是調用驅動程序方法來初始化ArrayList的

public static void main (String[] args) // main method 
{ 
    BMWdriver driver = new BMWdriver(); 
    driver.Driver(); 
    driver.startMenu(); 
    driver.inputCarDetails(); 
    driver.inputBikeDetails(); 
} 

}

您嘗試將數據添加到您的列表,它尚未初始化。 你應該確保它在添加任何數據之前被初始化。

+0

據我所知,雖然我不清楚驅動程序類中的初始化方法,但驅動程序類中的每個方法是否都需要初始化? – Dylan

+0

沒有什麼比如初始化方法。但是你的Driver方法負責初始化你的數組列表。而其他方法正在利用未初始化的數組列表。這會給出錯誤 – Priyamal

+0

好吧,我明白了。謝謝Priyamal! – Dylan

0

你拼錯了你的構造

public void Driver() 

這應該是

public BMWdriver() 

然後你list財產將被初始化,NPE添加到列表中時,已經一去不復返了。

相關問題