2

If app killed manually and try to sent update i am getting nullPointerException in API response. If I logout properly its working fine. I logged the input values everything is fine. anyone please look through this code

This is my API update.

private void updateFb(final String shop, final String phone,final String user_ids,final String lat, String lon,final String feedb) {

    Log.i("TAG", shop+","+phone+","+user_ids+","+lat+","+lon+","+feedb);
    new RetrofitHelper(MainActivity.this)
        .getApIs()
        .feedback(shop,phone,user_ids,lat,lon,feedb)
        .enqueue(new Callback<JsonElement>()
         {
                @Override
                public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
                    try {
                        JSONObject jsonObject=new JSONObject(response.body().toString());
                        String status=jsonObject.getString("status");
                        String date_time=jsonObject.getString("dt_time");
                        if (status=="Failed"){
                            Toasty.error(MainActivity.this, "Updating Failed", Toast.LENGTH_SHORT).show();
                        }
                        else {
                            shopName.setText("");
                            feedback.setText("");
                            phoneEdt.setText("");
                            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                            Toasty.success(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onFailure(Call<JsonElement> call, Throwable t) {

                }
            });

  // API interface
@FormUrlEncoded
    @POST("/web-api/insert_shop.php?")

    Call<JsonElement>feedback(@Field("shop_name") String shopName,@Field("phone")String phone, @Field("user_id") String user_id,
                              @Field("pos_lat") String pos_lat, @Field("pos_long") String pos_lon, @Field("feedback") String feedback);

Response i got .

java.lang.NullPointerException: Attempt to invoke interface method 'retrofit2.Call com.example.user.comcubeassist.retrofit.APIs.feedback(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)' on a null object reference

Dharmishtha
  • 1,313
  • 10
  • 21
  • Post the crash log . Probably the null context i think . – ADM Dec 06 '17 at 11:14
  • java.lang.NullPointerException: Attempt to invoke interface method 'retrofit2.Call com.example.user.comcubeassist.retrofit.APIs.feedback(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)' on a null object reference – Leo Johny Thayyil Dec 06 '17 at 11:18
  • Debug the code and Check for response as null before parsing it. Check for activity availability too by if(!isFinishing()). – ADM Dec 06 '17 at 11:20
  • when i debug its got exception from this line new RetrofitHelper(MainActivity.this).getApIs().feedback(shop,phone,user_ids,lat,lon,feedb) .enqueue(new Callback() – Leo Johny Thayyil Dec 06 '17 at 11:23
  • See the answer by rajan below. – ADM Dec 06 '17 at 11:27
  • its working good if app closed after logout. in case app killed after login and before logout, response becomes null – Leo Johny Thayyil Dec 06 '17 at 12:01

1 Answers1

1

Value of new RetrofitHelper(MainActivity.this).getApIs() is null, so when are you trying to access feedback method , it's throwing null reference. Verify getApIs() method and check why it is returning null.

Edit I guess you are initializing your Refrofit client on login page, hence if you kill and reopen app , it’s returning null . Try to initialise the client on home screen

Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
  • thanks for your response. but its working good if app closed after logout. in case app killed after login and before logout, response becomes null – Leo Johny Thayyil Dec 06 '17 at 11:58