Null object reference Android Azure -


i have problem azure mobile services on android, see exmaple todoitem , create instance called user to save data. code

public class user {  @com.google.gson.annotations.serializedname("email") private string email;  @com.google.gson.annotations.serializedname("password") private string password;  @com.google.gson.annotations.serializedname("name") private string name;  @com.google.gson.annotations.serializedname("lastname") private string lastname;  @com.google.gson.annotations.serializedname("phone") private string phone;  public user(){  } public user(string email, string password, string name, string lastname, string phone){      this.setemail(email);     this.setpassword(password);     this.setname(name);     this.setlastname(lastname);     this.setphone(phone); }  public string getemail() {     return email; }  public string getpassword() {     return password; }  public string getname() {     return name; }  public string getlastname() {     return lastname; }  public string getphone() {     return phone; }  public void setemail(string email) {     this.email = email; }  public void setpassword(string password) {     this.password = password; }  public void setname(string name) {     this.name = name; }  public void setlastname(string lastname) {     this.lastname = lastname; }  public void setphone(string phone) {     this.phone = phone; } 

}

and code of useractivity

public class useractivity extends activity {  private mobileserviceclient muser;  private mobileservicetable<user> mtodotable;   private edittext edittextemail; private edittext edittextmp; private edittext edittextname; private edittext edittextlastname; private edittext edittextphone;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_user);      edittextemail = (edittext) findviewbyid(r.id.edittextemail);     edittextmp = (edittext) findviewbyid(r.id.edittextmp);     edittextname = (edittext) findviewbyid(r.id.edittextname);     edittextlastname = (edittext) findviewbyid(r.id.edittextlastname);     edittextphone = (edittext) findviewbyid(r.id.edittextphone);     try {         // create mobile service client instance, using provided          // mobile service url , key         muser = new mobileserviceclient(                 "******",                 "******",                 this);       } catch (malformedurlexception e) {         createandshowdialog(new exception("there error creating mobile service. verify url"), "error");     }   } public user additemintable(user user) throws executionexception, interruptedexception {     user entity = mtodotable.insert(user).get();     return entity; }  private asynctask<void, void, void> runasynctask(asynctask<void, void, void> task) {     if (build.version.sdk_int >= build.version_codes.honeycomb) {         return task.executeonexecutor(asynctask.thread_pool_executor);     } else {         return task.execute();     } } public void additem(view view) {     if (muser == null) {         return;     }      final user user = new user();     user.setemail(edittextemail.gettext().tostring());     user.setpassword(edittextmp.gettext().tostring());     user.setname(edittextname.gettext().tostring());     user.setlastname(edittextlastname.gettext().tostring());     user.setphone(edittextphone.gettext().tostring());       // insert new item     asynctask<void, void, void> task = new asynctask<void, void, void>(){         @override         protected void doinbackground(void... params) {             try {                 final user entity = additemintable(user);             } catch (final exception e) {                 createandshowdialogfromtask(e, "error");             }             return null;         }     };      runasynctask(task);     edittextemail.settext("");     edittextmp.settext("");     edittextname.settext("");     edittextlastname.settext("");     edittextphone.settext(""); }  /**  * creates dialog , shows  *  * @param exception  *            exception show in dialog  * @param title  *            dialog title  */ private void createandshowdialogfromtask(final exception exception, string title) {     runonuithread(new runnable() {         @override         public void run() {             createandshowdialog(exception, "error");         }     }); }   /**  * creates dialog , shows  *  * @param exception  *            exception show in dialog  * @param title  *            dialog title  */ private void createandshowdialog(exception exception, string title) {     throwable ex = exception;     if(exception.getcause() != null){         ex = exception.getcause();     }     createandshowdialog(ex.getmessage(), title); }  /**  * creates dialog , shows  *  * @param message  *            dialog message  * @param title  *            dialog title  */ private void createandshowdialog(final string message, final string title) {     final alertdialog.builder builder = new alertdialog.builder(this);      builder.setmessage(message);     builder.settitle(title);     builder.create().show(); } private asynctask<void, void, void> initlocalstore() throws mobileservicelocalstoreexception, executionexception, interruptedexception {      asynctask<void, void, void> task = new asynctask<void, void, void>() {         @override         protected void doinbackground(void... params) {             try {                  mobileservicesynccontext synccontext = muser.getsynccontext();                  if (synccontext.isinitialized())                     return null;                  sqlitelocalstore localstore = new sqlitelocalstore(muser.getcontext(), "offlinestore", null, 1);                  map<string, columndatatype> tabledefinition = new hashmap<string, columndatatype>();                 tabledefinition.put("email", columndatatype.string);                 tabledefinition.put("password", columndatatype.string);                 tabledefinition.put("name", columndatatype.string);                 tabledefinition.put("lastname", columndatatype.string);                 tabledefinition.put("phone", columndatatype.string);                  localstore.definetable("user", tabledefinition);                  simplesynchandler handler = new simplesynchandler();                  synccontext.initialize(localstore, handler).get();              } catch (final exception e) {                 createandshowdialogfromtask(e, "error");             }              return null;         }     };      return runasynctask(task); } 

}

after click on button, exception shown: attempt invoke virtual method 'com.google.common.util.concurrent.listenablefutre com.microsoft.windowsazure.mobileservices.table.mobileservicetable.insert(java.lang.object)' on null object refrence. help

you declared field mtodotable in class, never initialized it. can initialize after creating mobileserviceclient instance:

try {     // create mobile service client instance, using provided      // mobile service url , key     muser = new mobileserviceclient(             "******",             "******",             this);     mtodotable = muser.gettable("user", user.class); } catch (...) 

you'll need add new member user class, id, represent identifier of user in database.

public class user {     // ...      @com.google.gson.annotations.serializedname("id")     private string id;      // can add get/set if want. } 

Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -