我遇到此错误java.lang.NullPointerException:
在Android中执行int java.lang.String.length()
时,尝试在空对象引用上调用虚方法onRefresh
。当我在onRefresh
方法之外获取数据时,它很好。
这是我的排球请求
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);
这是我的刷新方法
@Override
public void onRefresh() {
ViewTicketModel ticketModel = new ViewTicketModel();
String a = ticketModel.getmToken();
String b = ticketModel.getmSignature();
String c = ticketModel.getmPvdCode();
getDataTicket(a,b,c);
}
答案 0 :(得分:0)
@Override
public void onResponse(String response) {
try {
JSONObject ticketObj = new JSONObject(response);
JSONArray ticketArray = ticketObj.getJSONArray("a");
if (ticketArray != null)
for (int i = 0; i < ticketArray.length(); i++) {
...