1

This code works. it prints out the values in the list

ArrayList<String> menuItemsUrl=null;

for (int i = 0; i < modules.size(); i++) {
                String tmp = modules.get(i).attr("href");
                Log.d("", tmp);

            }

but this one generates an null pointer. why?

for (int i = 0; i < modules.size(); i++) {
                String tmp = modules.get(i).attr("href");
                menuItemsUrl.add(tmp);

            }

2 Answers 2

3

In your code you havent initialized menuItemsUrl, hence NPE.

You have declared your list here ArrayList<String> menuItemsUrl=null; and when you try to access it menuItemsUrl.add(tmp);, menuItemsUrl isn't refering to anything it is pointing to null which means nothing hence NPE, to get rid of NPE, initialize it before you use.

menuItemsUrl = new ArrayList<String>();
Sign up to request clarification or add additional context in comments.

Comments

0

The null point exception is occur at the "modules.size()" .The modules is a null reference. Try to instance the modules reference.

Comments

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.