I have a custom class "CarSharing", with an attribute "availableCars". An ArrayList availableCars is inizialized in the constuctor . When I create a CarSharing object and try to call the ArrayList in a method I get a java.lang.NullPointerException.
public class CarSharing {
private ArrayList<Car> availableCars;
public CarSharing() {
ArrayList<Car> availableCars = new ArrayList<Car>();
// I can use the List inside of the constructor
}
public void addAvailableCar(Car newCar) {
availableCars.add(newCar);
// Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at CarSharing.addVerfuegbaresAuto(CarSharing.java:12)
}
Any Ideas? Thank you.