Attempt to invoke virtual method on a null object reference - Firebase database android -
i trying read 'resname' , 'status' values pjsxhqxcyxdsv07lxvzu7buvhkf2 under business_info node firebase database, seen .
i keep getting error:
attempt invoke virtual method 'java.lang.string com.example.android.project_water.utilities.restaurantinformation.getresname()' on null object reference.
does know how solve this? please let me know if more info needed.
code:
public class info extends fragment { private databasereference mdatabase; private firebaseauth firebaseauth; private string businessid; private textview resnamechange, statuschange; public info() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_info, container, false); sharedpreferences businessid1 = getactivity().getsharedpreferences("business_id", context.mode_private); businessid = businessid1.getstring("businessid", "businessidnotfound"); log.i("business key is: ", businessid); firebaseauth = firebaseauth.getinstance(); mdatabase = firebasedatabase.getinstance().getreference(); resnamechange = rootview.findviewbyid(r.id.resnamechange); statuschange = rootview.findviewbyid(r.id.statuschange); mdatabase.addvalueeventlistener(new valueeventlistener() { @override public void ondatachange(datasnapshot datasnapshot) { showdata(datasnapshot); } @override public void oncancelled(databaseerror databaseerror) { } }); return rootview; } public void showdata(datasnapshot datasnapshot) { if (datasnapshot.child("business_info").child(businessid).exists()) { (datasnapshot ds : datasnapshot.getchildren()) { restaurantinformation resinfo = new restaurantinformation(); //error on line below****** resinfo.setresname(ds.child(businessid).getvalue(restaurantinformation.class).getresname()); resinfo.setstatus(ds.child(businessid).getvalue(restaurantinformation.class).getstatus()); resnamechange.settext(resinfo.getresname()); statuschange.settext(resinfo.getstatus()); } } else { resnamechange.settext("name"); statuschange.settext("online"); } } }
my model
public class restaurantinformation { private string resname; private string status; public restaurantinformation() { } public restaurantinformation(string resname) { this.resname = resname; } public restaurantinformation(string resname, string status) { this.resname = resname; this.status = status; } public string getresname() { return resname; } public void setresname(string resname) { this.resname = resname; } public string getstatus() { return status; } public void setstatus(string status) { this.status = status; } }
your current code downloading entire database , tries figure out show client-side. wasteful use of user's bandwidth. recommend listening business you're interested in:
mdatabase = firebasedatabase.getinstance().getreference(); mdatabase.child("business_info").child(businessid).addvalueeventlistener(new valueeventlistener() {
then can simplify showdata
method to:
public void showdata(datasnapshot snapshot) { if (snapshot.exists()) { restaurantinformation resinfo = snapshot.getvalue(restaurantinformation.class) resnamechange.settext(resinfo.getresname()); statuschange.settext(resinfo.getstatus()); } else { resnamechange.settext("name"); statuschange.settext("online"); } }
Comments
Post a Comment