Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitmapfun Sample code gives NullPointerException

Tags:

android

bitmap

Sample Source: http://developer.android.com/training/displaying-bitmaps/index.html

not sure why I'm not able to run the project at all.

Log Cat

09-06 13:14:13.664: E/AndroidRuntime(591): FATAL EXCEPTION: main
09-06 13:14:13.664: E/AndroidRuntime(591): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.bitmapfun/com.example.android.bitmapfun.ui.ImageGridActivity}: java.lang.NullPointerException
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.os.Looper.loop(Looper.java:137)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.app.ActivityThread.main(ActivityThread.java:4340)
09-06 13:14:13.664: E/AndroidRuntime(591):  at java.lang.reflect.Method.invokeNative(Native Method)
09-06 13:14:13.664: E/AndroidRuntime(591):  at java.lang.reflect.Method.invoke(Method.java:511)
09-06 13:14:13.664: E/AndroidRuntime(591):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-06 13:14:13.664: E/AndroidRuntime(591):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-06 13:14:13.664: E/AndroidRuntime(591):  at dalvik.system.NativeStart.main(Native Method)
09-06 13:14:13.664: E/AndroidRuntime(591): Caused by: java.lang.NullPointerException
09-06 13:14:13.664: E/AndroidRuntime(591):  at com.example.android.bitmapfun.util.DiskLruCache.getDiskCacheDir(DiskLruCache.java:268)
09-06 13:14:13.664: E/AndroidRuntime(591):  at com.example.android.bitmapfun.util.ImageCache.init(ImageCache.java:121)
09-06 13:14:13.664: E/AndroidRuntime(591):  at com.example.android.bitmapfun.util.ImageCache.<init>(ImageCache.java:61)
09-06 13:14:13.664: E/AndroidRuntime(591):  at com.example.android.bitmapfun.util.ImageCache.findOrCreateCache(ImageCache.java:107)
09-06 13:14:13.664: E/AndroidRuntime(591):  at com.example.android.bitmapfun.ui.ImageGridFragment.onCreate(ImageGridFragment.java:96)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:834)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:505)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.app.Activity.performStart(Activity.java:4475)
09-06 13:14:13.664: E/AndroidRuntime(591):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1928)
09-06 13:14:13.664: E/AndroidRuntime(591):  ... 11 more
like image 209
Jeff Huang Avatar asked Nov 03 '22 16:11

Jeff Huang


2 Answers

Were you trying to run this in an emulator?

My problem was that I had an AVD configuration with Galaxy Nexus and Nexus S but did not set up SD storage for them. You see these devices don't have SD cards that you can remove they only have internal memory but this memory is divided on "internal" partition for apps and "external" partition you can mount on your PC.

The code in Bitmapfun ImageCahcer class will call method Environment.isExternalStorageRemovable() and this will return false for mentiond devices but there will not be external storage since you haven't specified SD card size.

The solution is to set SD card size to some value and the code will work.

like image 184
Nemanja Kovacevic Avatar answered Nov 08 '22 08:11

Nemanja Kovacevic


Probably you are passing null as a key. You can check your code with these changes:

public static String hashKeyForDisk(String key) {
    String cacheKey;
    try {
        final MessageDigest mDigest = MessageDigest.getInstance("MD5");
        if (key == null) {
            Log.e("TEST","key = null");
            throw new IllegalArgumentException(" key == null");
        }

        if (mDigest == null) {
            Log.e("TEST","mDigest = null");

            return String.valueOf(key.hashCode());
        }
        byte[] bytes = key.getBytes();
        mDigest.update(bytes);
        cacheKey = bytesToHexString(mDigest.digest());
    } catch (NoSuchAlgorithmException e) {
        cacheKey = String.valueOf(key.hashCode());
    }
    return cacheKey;
}
like image 43
Roger Alien Avatar answered Nov 08 '22 10:11

Roger Alien