0

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.

1
  • I wonder why the down vote? Yes it is an easy catch but isnt SO for all types of question? easy or not Commented Nov 26, 2013 at 21:55

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, I cant believe I made that rookie mistake. Thanks for pointing it out ! I will accept your answer as soon as 10 min is over

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.