java - NullPointerException when making Volley-okhttp request -


i'm trying use google volley okhttp. following tutorial http://www.androidhive.info/2014/05/android-working-with-volley-library-1/ set up. set volley singleton , lrubitmapcache. used string request, everytime make request nullpointerexception.

7717-8280/com.admin.zipline e/androidruntime﹕ fatal exception: pool-9-thread-1 process: com.admin.zipline, pid: 7717 java.lang.nullpointerexception         @ com.admin.zipline.activities.accountverification.gettransactionsdetails(accountverification.java:295)         @ com.admin.zipline.activities.accountverification_.access$701(accountverification_.java:25)         @ com.admin.zipline.activities.accountverification_$8.execute(accountverification_.java:203)         @ org.androidannotations.api.backgroundexecutor$task.run(backgroundexecutor.java:302)         @ java.util.concurrent.executors$runnableadapter.call(executors.java:422)         @ java.util.concurrent.futuretask.run(futuretask.java:237)         @ java.util.concurrent.scheduledthreadpoolexecutor$scheduledfuturetask.access$201(scheduledthreadpoolexecutor.java:152)         @ java.util.concurrent.scheduledthreadpoolexecutor$scheduledfuturetask.run(scheduledthreadpoolexecutor.java:265)         @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112)         @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587)         @ java.lang.thread.run(thread.java:841) 

here's request

error on line 295 volleynetwork.getinstance().addtorequestqueue(strreq, tag_string_req);

@background public void gettransactionsdetails() {  string url = networkconstants.url_transactions; // tag used cancel 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 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 request queue if(volleynetwork.getinstance() == null){     log.i("null", "network null"); }else {     volleynetwork.getinstance().addtorequestqueue(strreq, tag_string_req); }  } 

my singleton set same, besides adding few global variables @ 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 default tag if tag 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 may have manifest. i'm not sure how set up. person before me set of app, , i'm not familiar java.

right manifest uses android.permission.internet permission, , name of application .activities.myapplication. myapplication extends application. tried set application name .network.volleynetwork, , have volleynetwork extend myapplication, got "volleynetwork has no default constructor."

not sure here.

this happens because you're not creating object, volleynetwork.

singleton pattern works 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, static getinstance method creates instance , makes sure stays stored in static field, while in case retrieves default value of null.

also, make sure make constructor private, instance can retrieved through public getinstance method, otherwise can call constructor , create many instances 1 feels.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -