2

I have a Adapter in my application which inflates posts and shows them in a ListView. Each post has an image, so it uses LruCache to store images.

The app works well completely, but it crashed two or three times and I couldn't realize what was the problem. Only last time, I could get this log from LogCat:

E/AndroidRuntime(2407): Caused by: java.lang.NullPointerException: key == null || value == null

The crashes happen only when app is recently installed (When I delete the app from the device and install it again)! When I open the app for the first time and start scrolling up and down so fast, sometimes it crashes. (It won't happen all the time, it's completely random!)

And after that, it won't crash anymore... :/

I think it has something to do with LruCache and empty cache. I saw this question, but that wasn't my problem! I did check for null cache in my code:

if (MainActivity.thumbnailsCache.get(postID) != null)
        {
            imageViewThumbnail.setImageBitmap(MainActivity.thumbnailsCache.get(postID));
        } else {
            new DownloadAndSetImageTask(thumbnailURL).execute();
        }

Any suggestion?

P.S.: I know my question doesn't have enough details, but that's all the information I have!

Community
  • 1
  • 1
Koorosh
  • 302
  • 1
  • 5
  • 15
  • This is probably the same as http://stackoverflow.com/questions/31362653/java-lang-nullpointerexception-in-android-util-lrucache-put-android – Henry Sep 29 '15 at 07:05

2 Answers2

4

According to LruCache.java line 164, LruCache throws a NullPointerException with that message in only one case - when you try to call put with a null key or value.

so i'd find out where put is being called on the cache and see what keys and values are being written there and go from there. alternatively, you can put a break point in LruCache.java's put method and see what happens.

ahmedre
  • 1,684
  • 1
  • 14
  • 18
1

Here I guess your are managing cache by yourself, I recommend to use Picasso instead of using any, this will manage network operations and cache automatically, you will have to write only single line of code and your random crashes will no more occurs.

Here is the link. http://square.github.io/picasso/

Akber
  • 521
  • 4
  • 10