java - Getting Null Pointer Exception when trying to get the value from global variable -
this class stores global variables
public class globalvariables extends application { private string usertoken; private string userid; private string username; public string getusername() { return username; } public string getuserid() { return userid; } public string getusertoken() { return usertoken; } public void setusername(string username) { this.username = username; } public void setuserid(string userid) { this.userid = userid; } public void setusertoken(string usertoken) { this.usertoken = usertoken; } }
now setting values in login class
globalvariables newobj = new globalvariables(); newobj.setuserid(user_values.getstring("id")); newobj.setusername(user_values.getstring("name")); system.out.println(newobj.getuserid());
i getting value in systemout ,,, when try fetch value class getting null pointer exception
protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.timetable); textview test = (textview) findviewbyid(r.id.testing); globalvariables newobj = new globalvariables(); system.out.println( newobj.getuserid()); }
instead of using
`globalvariables newobj = new globalvariables(); system.out.println( newobj.getuserid());`
use
system.out.println((globalvariables) getapplication()).getuserid());
Comments
Post a Comment