1

i'm trying to download and load images to my NetworkImageView in my android application.. but i'm getting this error

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.android.volley.toolbox.ImageLoader com.example.username.socialoid.AppController.getImageLoader()' on a null object reference

don't what's going on. Generally the null pointer happens because of declaration errors but i'm doing no mistake here.. please help..

Here are my code snippiest

    // user profile pic
    profilePic.setImageUrl(item.getProfilePic(), imageLoader);

and imageloader is in other class as follow

private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;

public ImageLoader getImageLoader() {
    getRequestQueue();
    if (mImageLoader == null) {
        getLruBitmapCache();
        mImageLoader = new ImageLoader(this.mRequestQueue, mLruBitmapCache);
    }

    return this.mImageLoader;
}

my LruBitmapCache class

 import com.android.volley.toolbox.ImageLoader.ImageCache;
 import android.graphics.Bitmap;
 import android.support.v4.util.LruCache;

 public class LruBitmapCache extends LruCache<String, Bitmap> implements
    ImageCache {
 public static int getDefaultLruCacheSize() {
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int cacheSize = maxMemory / 8;

    return cacheSize;
}

public LruBitmapCache() {
    this(getDefaultLruCacheSize());
}

public LruBitmapCache(int sizeInKiloBytes) {
    super(sizeInKiloBytes);
}

@Override
protected int sizeOf(String key, Bitmap value) {
    return value.getRowBytes() * value.getHeight() / 1024;
}

@Override
public Bitmap getBitmap(String url) {
    return get(url);
}

@Override
public void putBitmap(String url, Bitmap bitmap) {
    put(url, bitmap);
}
}

complete Log ::

11-30 15:35:39.635  27827-27827/com.example.hissamyousaf.socialoid E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.hissamyousaf.socialoid, PID: 27827
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hissamyousaf.socialoid/com.example.hissamyousaf.socialoid.Home}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hissamyousaf.socialoid/com.example.hissamyousaf.socialoid.NewsFeed}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.android.volley.toolbox.ImageLoader com.example.hissamyousaf.socialoid.AppController.getImageLoader()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
            at android.app.ActivityThread.access$900(ActivityThread.java:177)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5942)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
     Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hissamyousaf.socialoid/com.example.hissamyousaf.socialoid.NewsFeed}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.android.volley.toolbox.ImageLoader com.example.hissamyousaf.socialoid.AppController.getImageLoader()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
            at android.app.ActivityThread.startActivityNow(ActivityThread.java:2435)
            at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
            at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
            at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:776)
            at android.widget.TabHost.setCurrentTab(TabHost.java:435)
            at android.widget.TabHost.addTab(TabHost.java:262)
            at com.example.hissamyousaf.socialoid.Home.onCreate(Home.java:42)
            at android.app.Activity.performCreate(Activity.java:6289)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
            at android.app.ActivityThread.access$900(ActivityThread.java:177)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5942)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.android.volley.toolbox.ImageLoader com.example.hissamyousaf.socialoid.AppController.getImageLoader()' on a null object reference
            at com.example.hissamyousaf.socialoid.FeedListAdapter.<init>(FeedListAdapter.java:28)
            at com.example.hissamyousaf.socialoid.NewsFeed.onCreate(NewsFeed.java:43)
            at android.app.Activity.performCreate(Activity.java:6289)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
            at android.app.ActivityThread.startActivityNow(ActivityThread.java:2435)
            at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
            at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
            at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:776)
            at android.widget.TabHost.setCurrentTab(TabHost.java:435)
            at android.widget.TabHost.addTab(TabHost.java:262)
            at com.example.hissamyousaf.socialoid.Home.onCreate(Home.java:42)
            at android.app.Activity.performCreate(Activity.java:6289)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
            at android.app.ActivityThread.access$900(ActivityThread.java:177)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5942)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
deejay
  • 572
  • 4
  • 18
  • could you provide source of the getRequestQueue() and getLruBitmapCache() methods? – Mayur Raiyani Nov 30 '15 at 09:43
  • You `AppController` reference is `null` – laalto Nov 30 '15 at 09:43
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Raf Nov 30 '15 at 09:45
  • @MayurRaiyani please check the question, i've included LruBitmapCache() – deejay Nov 30 '15 at 09:54
  • have you initialized mRequestQueue and mLruBitmapCache? and please post the whole log – Ishan Nov 30 '15 at 09:55
  • @MayurRaiyani here is getRequestQueue public RequestQueue getRequestQueue() { if (mRequestQueue == null) { mRequestQueue = Volley.newRequestQueue(getApplicationContext()); } return mRequestQueue; } – deejay Nov 30 '15 at 09:55
  • just wait a sec @MayurRaiyani i'll post the log here.. – deejay Nov 30 '15 at 09:58
  • yes i've initialized RequestQueue and Bitmapcache private RequestQueue mRequestQueue; private ImageLoader mImageLoader; LruBitmapCache mLruBitmapCache; – deejay Nov 30 '15 at 10:00
  • Do you need to call init() on imageLoader? – Kenan Begić Nov 30 '15 at 16:24

0 Answers0