Retrofit给出空指针异常

时间:2017-11-23 04:54:46

标签: java android retrofit2

  

第一个状态代码为500的

{
"success": false,
"status": 500,
"message": "Salary detail not found."

}

  

第二个状态代码为200

{
"success": true,
"status": 200,
"result": {
    "salary": {
        "month_name": "October",
        "basic_salary": "Rs. 3,97,177.00",
        "overtime": "3987.5 (Hrs.)",
        "overtime_amount": "Rs. 1,56,696.00",
        "jobwork_amount": "Rs. 38,577.00",
        "net_salary": "Rs. 4,11,169.00"
    },
    "salary_list": [
        {
            "id": 279,
            "employee_name": "Rakesh [RAK]",
            "net_salary": "Rs. 10,911.00"
        }
    ]
}

}

  

这是模型类

public class SalaryListModel {

@SerializedName("success")
@Expose
public String success;

@SerializedName("status")
@Expose
public int status_code;

@SerializedName("message")
@Expose
private String message;

@SerializedName("result")
private Result result;


public String getMessage() {
    return message;
}

public String getSuccess() {
    return success;
}

public int getStatus_code() {
    return status_code;
}

public Result getResult() {
    return result;
}
  

这是改造

Call<SalaryListModel> call = apiInterface.getSalaryList(new SessionManagerInd(activity).basicAuthRetrofit(), url);
    Log.e(TAG, "retrofitCall: URL: " + url);
    call.enqueue(new Callback<SalaryListModel>() {
        @Override
        public void onResponse(Call<SalaryListModel> call, Response<SalaryListModel> response) {
            if (response.isSuccessful()) {
                Log.e(TAG, "onResponse: calling");
                if (response.body().getStatus_code() == 200) {
                    Log.e(TAG, "onResponse: calling 200");



                }
            } else {
                Log.e(TAG, "onResponse: else--");
                if (response.body().status_code == 500) {
                    Log.e(TAG, "onResponse: 500");

                } else if (response.body().getStatus_code() == 406) {
                    Log.e(TAG, "onResponse: 406");
                } else {
                    Log.e(TAG, "onResponse: esle");
                }

            }

        }


        @Override
        public void onFailure(Call<SalaryListModel> call, Throwable t) {
            Log.e(TAG, "onFailure: " + t.getMessage());

        }
    });
}
  

问题是当statuscode = 200时工作正常但是当状态代码为500时会出现错误

FATAL EXCEPTION: main
                                                                        Process: myapplication.erp.com.erpapp, PID: 5883
                                                                        java.lang.NullPointerException: Attempt to read from field 'int model.SalaryListModel.status_code' on a null object reference
                                                                            at cyberconsrots.com.sirgroup.sir_india.fragments.SalaryList$2.onResponse(SalaryList.java:195)
                                                                            at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory                                                            

现在我不知道我做错了什么。请帮助我从2小时开始坚持这个。状态代码为整数形式

2 个答案:

答案 0 :(得分:-1)

如果您的回复出错,则您的回复正文为空。 您必须使用Response中的代码,如果您想从错误中获取数据,请使用errorBody():

public void onResponse(Call<SalaryListModel> call, Response<SalaryListModel> response) {
            if (response.isSuccessful()) {
                Log.e(TAG, "onResponse: calling");
                if (response.body().getStatus_code() == 200) {
                    Log.e(TAG, "onResponse: calling 200");

                }
            } else {
                Log.e(TAG, "onResponse: else--");
                if (response.code() == 500) {
                    Log.e(TAG, "onResponse: 500");

                    //and if you want to parse your error body try this

                    /*
                    try {
                        Gson gson = new Gson();
                        YourErrorClass error = gson.fromJson(response.errorBody().charStream(), YourErrorClass.class);

                    } catch (Exception e) {
                         Log.e(TAG, "onResponse error: " + e.getMessage());
                    }*/



                } else if (response.code() == 406) {
                    Log.e(TAG, "onResponse: 406");
                } else {
                    Log.e(TAG, "onResponse: esle");
                }

            }

        }

答案 1 :(得分:-1)

你应该像那样使用它

if (response.isSuccessful()) {
            Log.e(TAG, "onResponse: calling");
            if (response.body().getStatus_code() == 200) {
                Log.e(TAG, "onResponse: calling 200");
            } else if (response.body().getStatus_code == 500) {
                Log.e(TAG, "onResponse: 500");
            } else if (response.body().getStatus_code() == 406) {
                Log.e(TAG, "onResponse: 406");
            } else {
                Log.e(TAG, "onResponse: esle");
            }
        } else {
            Log.e(TAG, "onResponse: UnSuccessful");
        }