I am trying to follow this guide on how to work with Volley using a Singleton. The goal is to use this Singleton to have my RequestQueue in an Application context so it won't be affected by shifting from landscape to portrait and such.
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) ...
And it points towards this line:
mRequestQueue = Volley.newRequestQueue(HelperApplication.getAppContext());
I am not sure what is wrong here as from what I can see in the code the getInstance() method in VolleySingleton.java is not supposed to
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;
}
}
This is the line of code I used from Volley before implementing the Singlton which worked perfectly but do not meet my needs:
RequestQueue queue = Volley.newRequestQueue(getActivity());
I am debugging on Genymotion. Fruthermore, row of code from Volley.java mentioned in the exception:
File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
I need help getting passed this error.
If you read the README of the Github repo that code links to, it specifically mentions, you need to add this to your manifest XML.
<application android:name="com.company.MyApplication">
</application>
In your case, change com.company.MyApplication to xxx.yyy.HelperApplication, though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With