Java - NullPointerException with initialized ArrayList -


this question has answer here:

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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -