0

I'm receiving a null value from my hashmap. This is the hashmap creation:

private HashMap<String,Bitmap> thumbs = new HashMap<String,Bitmap>();

/* adding a single value to the hashmap */

Then I proceed to retreiving the value from the hashmap, like so:

    public Bitmap getImageByFileName(String fileName) {

    Bitmap fish = null;
    Iterator it = thumbs.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pairs = (Map.Entry)it.next();
        fish = (Bitmap)thumbs.get(fileName);        
        it.remove(); 
    }   
    Log.i("shnitzel", " bitmap is " + fish);
    fish = (Bitmap)thumbs.get(fileName);
    Log.i("shnitzel", " final bitmap is " + fish);
    return fish;
}

The log file:

08-05 22:18:28.170: I/shnitzel(477):  bitmap is android.graphics.Bitmap@40650138
08-05 22:18:28.170: I/shnitzel(477):  final bitmap is null

As you can see, I use the exact same command inside and outside the 'while' loop, but for some reason it works inside it, but not outside. Why does this happen?

3
  • 6
    Are you not removing it in the while loop? Commented Aug 5, 2013 at 19:28
  • :( Embarrassing indeed. I actually only added this while loop to see why I was receiving a null value to begin with. I honestly have no idea why it didn't work before, but does now. Thanks anyway :) Commented Aug 5, 2013 at 19:36
  • Glad to hear the problem's solved! Please select an answer to close your question. Commented Aug 5, 2013 at 21:52

2 Answers 2

3

In your loop:

it.remove(); 

You're removing the element after having read it.

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

Comments

1

it.remove() is removing the entry from the HashMap.

According to the documentation:

The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.

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.