Java – Android Volley bug using singleton mode

Android Volley bug using singleton mode… here is a solution to the problem.

Android Volley bug using singleton mode

I’m trying to follow This guide on how to use Singleton to work with Volley. The goal is to use this Singleton to have my RequestQueue in the context of the application so that it is not affected by the transition from landscape to portrait, etc.

But I get the following error:

java.lang.NullPointerException: Attempt to invoke virtual method ‘java.io.File android.content.Context.getCacheDir()’ on a null object reference
at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:45)
at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:105)
at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:115)
at se.ccconsulting.arenaui3.VolleySingleton. (VolleySingleton.java:20)
at se.ccconsulting.arenaui3.VolleySingleton.getInstance(VolleySingleton.java:34)
at se.ccconsulting.arenaui3.AdventureFragment.onCreateView(AdventureFragment.java:62)

It points to this line:

mRequestQueue = Volley.newRequestQueue(HelperApplication.getAppContext());

I’m not sure what’s wrong here, because from what I’ve seen in the code, the getInstance() method in VolleySingleton.java shouldn’t

VolleySingleton.java

public class VolleySingleton {
    private static VolleySingleton mInstance = null;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;

private VolleySingleton(){
            mRequestQueue = Volley.newRequestQueue(HelperApplication.getAppContext());
            mImageLoader = new ImageLoader(this.mRequestQueue, new ImageLoader.ImageCache() {
            private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10);
            public void putBitmap(String url, Bitmap bitmap) {
                mCache.put(url, bitmap);
            }
            public Bitmap getBitmap(String url) {
                return mCache.get(url);
            }
        });
    }

public static VolleySingleton getInstance(){
        if(mInstance == null){
            mInstance = new VolleySingleton();
        }
        return mInstance;
    }

public RequestQueue getRequestQueue(){
        return this.mRequestQueue;
    }

public ImageLoader getImageLoader(){
        return this.mImageLoader;
    }
}

HelperApplication.java

public class HelperApplication extends Application{
    private static HelperApplication mInstance;
    private static Context mAppContext;

@Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;

this.setAppContext(getApplicationContext());
    }

public static HelperApplication getInstance(){
        return mInstance;
    }
    public static Context getAppContext() {
        return mAppContext;
    }
    public void setAppContext(Context mAppContext) {
        this.mAppContext = mAppContext;
    }
}

Here is the line of Volley code I used before implementing Singlton, it worked fine but didn’t meet my needs :

RequestQueue queue = Volley.newRequestQueue(getActivity());

I’m debugging on Genymotion.
Also, a line of code from Volley.java mentioned in the exception:

File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);

I need help to overcome this error.

Solution

If you read Github repo The code links to, it specifically mentions that you need to add it to your list XML.

<application android:name="com.company.MyApplication">

</application>

However, in your case, change com.company.MyApplication to xxx.yyy.HelperApplication.

Related Problems and Solutions