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 ‘ android.content.Context.getCacheDir()’ on a null object reference
at se.ccconsulting.arenaui3.VolleySingleton. (
at se.ccconsulting.arenaui3.VolleySingleton.getInstance(
at se.ccconsulting.arenaui3.AdventureFragment.onCreateView(

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 shouldn’t

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;

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

    public void onCreate() {
        mInstance = this;


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 mentioned in the exception:

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

I need help to overcome this error.


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="">


However, in your case, change to xxx.yyy.HelperApplication.

Related Problems and Solutions