java - Null pointer exception when trying to pass a customAdapter? -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
public class jsonviewpager { private volleysingleton mvolleysingleton; private viewpageradapter mviewpageradapter; private requestqueue mrequestqueue; private string murl; private arraylist<questiondata> mquestiondataarraylist = new arraylist<questiondata>(); public jsonviewpager(viewpageradapter viewpageradapter,string url){ mviewpageradapter=viewpageradapter; murl=url; } public void jsonrequestmethod() { mvolleysingleton = volleysingleton.getinstance(); mrequestqueue = mvolleysingleton.getrequestqueue(); jsonarrayrequest request = new jsonarrayrequest(request.method.get, murl, (string) null, new response.listener<jsonarray>() { @override public void onresponse(jsonarray response) { parse parsejsonresponse=new parse(); mquestiondataarraylist = parsejsonresponse(response); mviewpageradapter.setbloglist(mquestiondataarraylist); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { } }); mrequestqueue.add(request); } }
i have class in passing viewpageradapter from, set viewpageradapter in oncreateview in other class, reason getting null pointer exception when @ mviewpageradapter.setbloglist(mquestiondataarraylist); line. know causing this?
home class
public class home extends fragment { private static final int refresh_delay=500; private static final string arg_param1 = "param1"; private static final string arg_param2 = "param2"; //url reflects ordered list on server filter terms {"include": {"relation": "questions", "scope": {"order" : "points desc"}}}) private static final string url_home="hiddenurl"; private string mparam1; private string mparam2; private imageview mimageview; private volleysingleton mvolleysingleton; private requestqueue mrequestqueue; private recyclerview mrecyclerview; private arraylist<questiondata> mlistblogs = new arraylist<>(); private arraylist<questiondata> mquestiondataarraylist = new arraylist<questiondata>(); private pulltorefreshview mpulltorefreshview; private viewpageradapter mviewpageradapter; public static home newinstance(string param1, string param2) { home fragment = new home(); bundle args = new bundle(); args.putstring(arg_param1, param1); args.putstring(arg_param2, param2); fragment.setarguments(args); return fragment; } public home() { // required empty public constructor } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if (getarguments() != null) { mparam1 = getarguments().getstring(arg_param1); mparam2 = getarguments().getstring(arg_param2); } //if wish handle event source in activity main thread, you’ll need add following instructions @ beginning of oncreate() android callback: system.out.println("userid" + login.getuserid()); system.out.println("username" + login.getusername()); jsonviewpager s=new jsonviewpager(mviewpageradapter,url_home); s.jsonrequestmethod(); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view view = inflater.inflate(r.layout.fragment_home, container, false); mrecyclerview = (recyclerview) view.findviewbyid(r.id.fragment_dashboard_recycler); mrecyclerview.setlayoutmanager(new linearlayoutmanager(getactivity())); mviewpageradapter = new viewpageradapter(getactivity()); mrecyclerview.setadapter((mviewpageradapter)); mpulltorefreshview = (pulltorefreshview) view.findviewbyid(r.id.pull_to_refresh); mpulltorefreshview.setonrefreshlistener(new pulltorefreshview.onrefreshlistener() { @override public void onrefresh() { mpulltorefreshview.postdelayed(new runnable() { @override public void run() { mpulltorefreshview.setrefreshing(false); jsonrequestmethod(); } }, refresh_delay); } }); return view; } }
you calling
jsonviewpager s=new jsonviewpager(mviewpageradapter,url_home);
in oncreate
method, , then
mviewpageradapter = new viewpageradapter(getactivity());
in oncreateview
method.
the problem in activity lifecycle, oncreateveiw
come after oncreate
, meaning jsonviewepager object created passing in null value of mviewpageradapter
.
for reference:
Comments
Post a Comment