Reputation: 283
I'm trying to use Google Volley with okHttp. I following this tutorial http://www.androidhive.info/2014/05/android-working-with-volley-library-1/ to set everything up. I set up my volley singleton and LruBitmapCache. Used their get string request, but everytime I make a request I get NullPointerException.
7717-8280/com.admin.zipline E/AndroidRuntime﹕ FATAL EXCEPTION: pool-9-thread-1
Process: com.admin.zipline, PID: 7717
java.lang.NullPointerException
at com.admin.zipline.activities.AccountVerification.getTransactionsDetails(AccountVerification.java:295)
at com.admin.zipline.activities.AccountVerification_.access$701(AccountVerification_.java:25)
at com.admin.zipline.activities.AccountVerification_$8.execute(AccountVerification_.java:203)
at org.androidannotations.api.BackgroundExecutor$Task.run(BackgroundExecutor.java:302)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Here's my request
Error is on line 295 which is VolleyNetwork.getInstance().addToRequestQueue(strReq, tag_string_req);
@Background
public void getTransactionsDetails() {
String url = NetworkConstants.Url_transactions;
// Tag used to cancel the request
String tag_string_req = "string_req";
Log.i("URL",url);
StringRequest strReq = new StringRequest(Request.Method.GET,
url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("Response", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("Error", "Error: " + error.getMessage());
}
}){
/**
** Passing some request headers
* */
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", VolleyNetwork.authorization);
return headers;
}
};
// Adding request to request queue
if(VolleyNetwork.getInstance() == null){
Log.i("NULL", "NETWORK NULL");
}else {
VolleyNetwork.getInstance().addToRequestQueue(strReq, tag_string_req);
}
}
My singleton is set up almost exactly the same, besides adding a few global variables at the top.
VolleyNetwork.java
public class VolleyNetwork extends Application {
Context context;
public static String access_token,token_type,user_id,name, authorization;
public VolleyNetwork(Context context) {
this.context = context;
SharedPreferences preferences = context.getSharedPreferences(FileNames.login_details, Context.MODE_PRIVATE);
access_token = preferences.getString(NetworkConstants.acess_token, "");
token_type = preferences.getString(NetworkConstants.token_type, "");
user_id = preferences.getString(NetworkConstants.user_id, "");
authorization = token_type + " " + access_token;
}
public static final String TAG = VolleyNetwork.class
.getSimpleName();
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static VolleyNetwork mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized VolleyNetwork getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public ImageLoader getImageLoader() {
getRequestQueue();
if (mImageLoader == null) {
mImageLoader = new ImageLoader(this.mRequestQueue,
new LruBitmapCache());
}
return this.mImageLoader;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
I think it may have something to do with my manifest. I'm not really sure how to set that up. The person before me set up most of the app, and I'm not that familiar with Java.
Right now the manifest uses the android.permission.INTERNET permission
, and the name of the application is .activities.MyApplication
. MyApplication extends Application. I tried to set the application name to .network.VolleyNetwork
, and have VolleyNetwork extend MyApplication, but got "VolleyNetwork has no default constructor."
Not really sure what to do here.
Upvotes: 1
Views: 638
Reputation: 1220
This happens because you're not actually creating your object, VolleyNetwork.
Singleton pattern works like this:
public class MySingleton {
private static MySingleton instance;
private MySingleton(){
//constructor here
}
public static MySingleton getInstance(){
if(instance==null){
instance = new MySingleton();
}
return instance;
}
}
Please note, that the static getInstance
method actually creates your instance and makes sure it stays stored in the static field, while in your case it retrieves the default value of null.
Also, make sure to make the constructor private, so that an instance can be retrieved only through the public getInstance
method, otherwise anyone can just call the constructor and create as many instances as one feels.
Upvotes: 1