简体   繁体   中英

Can't understand why i'm getting a null pointer error

I'm new enough to java coding and new to stack exchange. I have been trying to do this car dealership program as practice. i keep getting a null pointer error when trying to add either a car or a bike to an arraylist of vehicles and i cannot seem to figure out why, im almost positve that i have things mixed up and in the wrong place but i cant figure out which goes where. I'm using a driver, a Vehicles superclass and Cars and Bikes as two subclasses inheriting from Vehicles. Apologies if im unclear on anything, any help is appreciated. Thanks!

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();
}
}
ArrayList<Vehicles> list; 

you are not calling Driver method to initialize the arraylist

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

}

you are trying to add data to your list which has not been initialized. you should make sure that it is initialized before adding any data.

You misspelled your constructor

public void Driver()

This should be

public BMWdriver()

Then your list property will be initialized and the NPE when adding to the list is gone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM