i am having this error java.lang.NullPointerException:
Attempt to invoke virtual method int java.lang.String.length()
on a null object reference while executing the onRefresh
in Android. when i fetch data outside onRefresh
method it is fine.
here is my Volley Request
final String ticketUrl = "http";
StringRequest getTicket = new StringRequest(Request.Method.POST, ticketUrl,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject ticketObj = new JSONObject(response);
JSONArray ticketArray = ticketObj.getJSONArray("a");
for (int i = 0 ; i < ticketArray.length() ; i++){
ViewTicketModel ticket_object = new ViewTicketModel();
JSONObject ticketVal = ticketArray.getJSONObject(i);
String diagnosa = ticketVal.getString("first");
String noPasien = ticketVal.getString("second");
String dokter = ticketVal.getString("3rd");
String jenisPerawatan = ticketVal.getString("4th");
ticket_object.setJenis_perawatan(jenisPerawatan);
ticket_object.setNomor_pasien(noPasien);
ticket_object.setDoctor(dokter);
ticket_list_mdl.add(ticket_object);
adapter.notifyDataSetChanged();
// stopping swipe refresh
swipeRefreshView.setRefreshing(false);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.v("V_error",error.toString());
// stopping swipe refresh
// swipeRefreshView.setRefreshing(false);
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("a", token);
params.put("b", signature);
params.put("c", pvd_code);
return params;
}
};
RequestQueue ticketQueue = Volley.newRequestQueue(ViewTickets.this);
ticketQueue.add(getTicket);
and here is my on refresh method
@Override
public void onRefresh() {
ViewTicketModel ticketModel = new ViewTicketModel();
String a = ticketModel.getmToken();
String b = ticketModel.getmSignature();
String c = ticketModel.getmPvdCode();
getDataTicket(a,b,c);
}