0

I am trying to create an application which retrieves the users favourite book quote however I am stuck on how to display this information back to the user. I created an ArrayList which will store all the information. However when displaying this information, I keep getting the error:

java.lang.NullPointerException

when it tries to execute the code

temp[i] = new HashMap<String,String>();

This class is shown below:

public class FavouriteQuotesActivity extends ListActivity {

    static final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();

    private void getFavorites() {
        DataBaseHelper myDbHelper = new DataBaseHelper(this);

        String favorites [] = myDbHelper.getFavourites();

        if(list.size() > 0)
        {
            list.removeAll(list);
        }

        for(int i = 0;i < favorites.length; i++)
        {
            String quotes = favorites[i];
            String[] quoteArray = quotes.split(":");
            HashMap<String,String> temp[] = null; 

            temp[i] = new HashMap<String,String>();
            temp[i].put("Author",(quoteArray[2]));
            temp[i].put("Quote",(quoteArray[4]));
            list.add(temp[i]);

        }


    }

Any help would be greatly appreciated.

0

3 Answers 3

6

Look at this code:

HashMap<String,String> temp[] = null; 
temp[i] = new HashMap<String,String>();

You've just assigned a null value to the temp variable (which would more typically have a declaration of HashMap<String, String>[] temp) - so of course you'll get a NullPointerException when you then try to access temp[i] on the very next statement.

It's not clear why you're using an array at all in that code - why aren't you just using:

HashMap<String, String> map = new HashMap<String, String>();
map.put("Author", quoteArray[2]);
map.put("Quote", quoteArray[4]);
list.add(map);

Additionally it's unclear why you're using a map at all here, given that it will always have the same two keys. Why not create a Quote class with name and author properties? Also, using a static variable here seems like a bad idea - why doesn't getFavorites create a new list on each invocation, and return it at the end of the method?

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

3 Comments

The problem is that temp should not even be an array in the first place.
@Tudor: Hence the second part of my answer... it's not clear that a map is really needed either.
@user983965: But do you understand why the change was needed? Have you considered the questions in the final paragraph?
3

You are declaring and using temp wrong. You don't need an array of HashMap, just a single HashMap, since list is an ArrayList<HashMap>:

HashMap<String,String> temp = new HashMap<String,String>();
temp.put("Author",(quoteArray[2]));
temp.put("Quote",(quoteArray[4]));
list.add(temp);

Comments

3

temp doesn't point to an array. You are setting it to null.

You declared the variable temp as an array (by writing HashMap temp[]).

Without being it an actual array, you can't set any elements.

7 Comments

It is initialized - with the null value. That's a perfectly valid initial value... it's just you can't then dereference it...
I'd still bicker with the details in the second part: temp is a variable. Its value is either null, or a reference to an array. When the value is null, you can't set any elements.
@Jon: Indeed. Is there a nice name for what I meant to wrote. Apparently not "initialised". A word that says it is a null pointer?
How about "it's being initialized with a value of null"? After all, that's what you really mean :)
It has a value of null, it's as simple as that. The variable is definitely assigned (so code is able to read the value) - it's just that the value is null. It was previously assigned a non-null reference, and the second assignment changes the value to a null reference.
|

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.