0

In global definition I have declared: private HashMap<String, Bitmap> map = new HashMap<String, Bitmap>();

In other part of my code, I connect to server and get my required info. Two of then are image address (url) and image id. after that I download image and I want to assign it its own Id. This is my code:

private LinkedList<Bitmap> getFlagImages() {
    InputStream is= null;
    LinkedList<Bitmap> llBitmap = new LinkedList<Bitmap>();


    for(int i = 0; i < flag.getTeamLogo44x44().size(); i++) {
        String urlstr = flag.getTeamLogo44x44().get(i);

        try {
            HttpGet httpRequest   = new HttpGet(urlstr);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

            HttpEntity entity = response.getEntity();
            BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
            is = bufHttpEntity.getContent();
            Bitmap bm = BitmapFactory.decodeStream(is);

            llBitmap.add(bm);
            map.put(flag.getTeamId().get(i), bm);  // *Crash happens here

        }catch ( MalformedURLException e ){
            Log.d( "RemoteImageHandler", "Invalid URL passed" + urlstr );
        }catch ( IOException e ){
            Log.d( "RemoteImageHandler", "fetchImage IO exception: " + e );
        }finally{
            if(is != null) {
                try{
                    is.close();
                } catch(IOException e) {}
            }
        }
    }

    return llBitmap;        
}

When I run, the application crashes and logcat shows Null Pointer Exception and points to line map.put(flag.getTeamId().get(i), bm);

Any suggestion would be appreciated.

// Update, Also I used map.put(flag.getTeamId().get(i), Bitmap.createBitmap(bm)); but the result was same.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Hesam
  • 52,260
  • 74
  • 224
  • 365

3 Answers3

0

Looks like flag.getTeamId() is null, or, like cheeken said, flag.getTeamId.get(i) is null

You could try using assertions like assert (flag.getTeamId() != null) assert (flag.getTeamId().get(i) != null)

Now run your jvm with the -ea flag (short for enable assertions)

user3001
  • 3,437
  • 5
  • 28
  • 54
  • i'm sure it has result because i checked it. Now the problem solved with changing mentioned line with map.put(flag.getTeamId().get(i), Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight())); Thanks for your help – Hesam May 25 '12 at 03:06
0

I changed

map.put(flag.getTeamId().get(i), bm);

to

map.put(flag.getTeamId().get(i), Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight()));

and its ok, now. but I have no idea why the first one didn't work!!!

Hesam
  • 52,260
  • 74
  • 224
  • 365
0

It looks like you found your answer. However, you might consider having your map take WeakReference<Bitmap> as a value instead. That is,

Map<Integer, WeakReference<Bitmap>>

By keeping a weak reference, you ensure garbage collection will work as expected later on.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250