Java - NullPointerException with initialized ArrayList -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
i have custom class "carsharing", attribute "availablecars". arraylist availablecars inizialized in constuctor . when create carsharing object , try call arraylist in method java.lang.nullpointerexception.
public class carsharing { private arraylist<car> availablecars; public carsharing() { arraylist<car> availablecars = new arraylist<car>(); // can use list inside of constructor } public void addavailablecar(car newcar) { availablecars.add(newcar); // exception in thread "awt-eventqueue-0" java.lang.nullpointerexception @ carsharing.addverfuegbaresauto(carsharing.java:12) }
any ideas? thank you.
you don't initialize availablecars making local variable in constructor. fix simple remove arraylist in constructor.
private arraylist<auto> availablecars; public carsharing() { availablecars = new arraylist<car>(); }
Comments
Post a Comment