Eli Eli - 6 months ago 14
Android Question

Null pointer exception when trying to pass a customAdapter?

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 another class in which I am passing a viewPagerAdapter from, I set the viewPagerAdapter in the onCreateView in my other class, for some reason I am getting a null pointer exception when at mViewpagerAdapter.setBloglist(mQuestionDataArrayList); line. Does anyone know what is 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 you wish to handle you event source in the activity main thread, you’ll need to add the following instructions at the beginning of the 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 the layout for this 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;
}
}

Answer

You are calling

JsonViewpager s=new JsonViewpager(mViewpagerAdapter,URL_HOME);

in the onCreate method, and then

mViewpagerAdapter = new ViewpagerAdapter(getActivity());

in the onCreateView method.

The problem is that in the activity lifecycle, onCreateVeiw will come AFTER onCreate, meaning that your JsonViewepager object was created passing in a null for the value of mViewpagerAdapter.

For reference:

enter image description here