简体   繁体   中英

Null pointer exception upon adding an object to a list

I have a class 'AdResources'. I created a list to hold AdResources objects

private List<AdResources> adResourcesList = null;

Then I created an AdResources object AdResources map = new AdResources(); I also added some data to map by map.setType(((JSONArray)nearbyPlacesData.get(i)).optString(0)); (Nothing is null here, cross checked)

After all this I do adResourcesList.add(map); . I get null pointer exception here. This is weird because running a debugger, just before executing adResourcesList.add(map) I can see that the map object is not null, hence why the NPE?

Also to be noted is, I am running this from an AsyncTask doInBackground.

You have to initialize the list itself before adding.

private List<AdResources> adResourcesList = new ArrayList<AdResources>();

You clearly show that the list is null prior to adding it, so obviously it will throw a NullPointerException when you try to add something.

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