nullpointerexception - Attempt to invoke virtual method 'java.io.File android.content.Context.getCacheDir()' on a null object reference -


this question has answer here:

i trying use volley , send request check data after login.
have special class 'api' where's volley staff.
trying after checking validation in loginactivity (default android studio template) want create api instance in loginactivity , send request server.
testing volley, later want create 1 api instance in main class.

here's api class:

import android.app.application; import android.util.log;  import com.android.volley.*; import com.android.volley.toolbox.volley; import com.android.volley.toolbox.jsonobjectrequest; import org.json.jsonobject; import java.lang.string;  public class api extends application {      protected string serverurl;     protected string get;     protected string post;     protected requestqueue queue;      public api(){         setserverurl("http://localhost:63424");         setget("http://localhost:63424/api");         setpost("http://localhost:63424/api");         queue = volley.newrequestqueue(this);     }      protected void sendget (string request)     {         jsonobjectrequest getrequest = new jsonobjectrequest(request.method.get, request, null,                 new response.listener<jsonobject>()                 {                     @override                     public void onresponse(jsonobject response) {                         // display response                         log.d("responseapi", response.tostring());                     }                 },                 new response.errorlistener()                 {                     @override                     public void onerrorresponse(volleyerror error) {                         log.d("error.responseapi", error.tostring());                     }                 }         );         queue.add(getrequest);     }       //logowanie     public boolean checklogin (string login, string password){         setget(getget()+"/search_users_/login=/"+login + "/");         sendget(getget());          return true;      }      public string getserverurl() {         return serverurl;     }      public void setserverurl(string serverurl) {         serverurl = serverurl;     }      public string getget() {         return get;     }      public void setget(string get) {         this.get = get;     }      public string getpost() {         return post;     }      public void setpost(string post) {         this.post = post;     }  } 

here trying create api instance in loginactivity:

 public class userlogintask extends asynctask<void, void, boolean> {          private final string memail;         private final string mpassword;          userlogintask(string email, string password) {             memail = email;             mpassword = password;         }          @override         protected boolean doinbackground(void... params) {             // todo: attempt authentication against network service.              try {                 // simulate network access.                 thread.sleep(2000);             } catch (interruptedexception e) {                 return false;             }             log.d("api", "zaraz utworze api");             api api = new api();             return api.checklogin(memail, mpassword);              // todo: register new account here.          }          @override         protected void onpostexecute(final boolean success) {             mauthtask = null;             showprogress(false);              if (success) {                 finish();             } else {                 mpasswordview.seterror(getstring(r.string.error_incorrect_password));                 mpasswordview.requestfocus();             }         }          @override         protected void oncancelled() {             mauthtask = null;             showprogress(false);         }     } 

this solution not work, error:

fatal exception: asynctask #1                                                             process: pawel.cooker, pid: 2646                                                             java.lang.runtimeexception: error occurred while executing doinbackground()                                                                 @ android.os.asynctask$3.done(asynctask.java:353)                                                                 @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:383)                                                                 @ java.util.concurrent.futuretask.setexception(futuretask.java:252)                                                                 @ java.util.concurrent.futuretask.run(futuretask.java:271)                                                                 @ android.os.asynctask$serialexecutor$1.run(asynctask.java:245)                                                                 @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1162)                                                                 @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:636)                                                                 @ java.lang.thread.run(thread.java:764)                                                              caused by: java.lang.nullpointerexception: attempt invoke virtual method 'java.io.file android.content.context.getcachedir()' on null object reference                                                                 @ android.content.contextwrapper.getcachedir(contextwrapper.java:256)                                                                 @ com.android.volley.toolbox.volley.newrequestqueue(volley.java:43)                                                                 @ com.android.volley.toolbox.volley.newrequestqueue(volley.java:78)                                                                 @ pawel.cooker.api.<init>(api.java:17)                                                                 @ pawel.cooker.loginactivity$userlogintask.doinbackground(loginactivity.java:320)                                                                 @ pawel.cooker.loginactivity$userlogintask.doinbackground(loginactivity.java:299)                                                                 @ android.os.asynctask$2.call(asynctask.java:333)                                                                 @ java.util.concurrent.futuretask.run(futuretask.java:266)                                                                 @ android.os.asynctask$serialexecutor$1.run(asynctask.java:245)                                                                  @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1162)                                                                  @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:636)                                                                  @ java.lang.thread.run(thread.java:764)  

there's problem volley, have no idea what's wrong.

there's problem volley

no, there's problem api class.

make sure put in manifest

<application     android:name=".api" 

after that

  1. don't use constructor. use oncreate . or, don't use application, , use volley-scoped singleton -- see example documentation
  2. replace localhost actual server address

public class api extends application {      public static final string serverurl = "http://<use real server here>:63424";     public static final string = serverurl+"/api";     public static final string post = serverurl+"/api";     protected requestqueue queue;      private final api minstance;      private api() { }      public api getinstance() {         return minstance;     }      @override     public void oncreate(){         super.oncreate();         minstance = this;         queue = volley.newrequestqueue(this);     } 
  1. you can't return blocking value async functions. use callbacks pass results

protected void sendget (string url, response.listener<jsonobject> onsuccess) {     jsonobjectrequest getrequest = new jsonobjectrequest(request.method.get, url, null,             onsuccess,               new response.errorlistener()             {                 @override                 public void onerrorresponse(volleyerror error) {                     log.d("error.responseapi", error.tostring());                 }             }     );     queue.add(getrequest); }  public void checklogin (string login, response.listener<jsonobject> onsuccess){     sendget(get+"/search_users_/login=/"+login, onsuccess); } 
  1. don't use new api() when should instead use ((api) getapplicationcontext()) or above implementation, api.getinstance()

for example,

api api = api.getinstance().checklogin(memail, new response.listener<jsonobject>() {     @override     public void onresponse(jsonobject response) {         // todo: register new account here.     } }); 
  1. most importantly here, don't need asynctask when using volley. it's asynchronous

on related note, if using restful apis, retrofit might more useful


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -