0

I am getting null pointer exception problem on MyFirebaseInstanceIDService class but only for few devices or only few times. I cannot repeat the problem in my device so it is making me difficult to catch the problems. I have used try catch but still there exist the problems. Please help me find the problem. Thanks in advance.

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
    Response response;
    SharedPreferences sharedPrefs;
    SharedPreferences.Editor ed;
    @Override
    public void onTokenRefresh() {

        String token = FirebaseInstanceId.getInstance().getToken();

        sharedPrefs = getSharedPreferences("settings", MODE_PRIVATE);
        ed = sharedPrefs.edit();
        ed.putString("tokana", token);
        ed.commit();
        registerToken(token);
    }

    private void registerToken(String token) {
        OkHttpClient client = new OkHttpClient();
        RequestBody body = new FormBody.Builder()
                .add("Token",token)
                .build();

        Request request = new Request.Builder()
                .url("http://example.com/register.php")
                .post(body)
                .build();

        try {

           response = client.newCall(request).execute();
           // System.out.println("working...");

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            response.body().close();
        }

    }
}

This problem is shown on some devices. The following is the crash report from play store.

java.lang.NullPointerException: 

at com.ramropatro.app.MyFirebaseInstanceIDService.onTokenRefresh (MyFirebaseInstanceIDService.java)
or                     .registerToken (MyFirebaseInstanceIDService.java)

at com.ramropatro.app.MyFirebaseInstanceIDService.onTokenRefresh (MyFirebaseInstanceIDService.java)
or                     .registerToken (MyFirebaseInstanceIDService.java)

at com.google.firebase.iid.FirebaseInstanceIdService.onTokenRefresh (FirebaseInstanceIdService.java)
or                     .zza (FirebaseInstanceIdService.java)
or                     .zzbI (FirebaseInstanceIdService.java)
or                     .zzca (FirebaseInstanceIdService.java)
or                     .zzhi (FirebaseInstanceIdService.java)
or                     .zzo (FirebaseInstanceIdService.java)

at com.google.firebase.iid.FirebaseInstanceIdService.handleIntent (FirebaseInstanceIdService.java)
or                     .zzbK (FirebaseInstanceIdService.java)
or                     .zzbZ (FirebaseInstanceIdService.java)

at com.google.firebase.iid.zzc.run (zzc.java)

at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1112)

at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:587)

at java.lang.Thread.run (Thread.java:818)
Bishwash
  • 854
  • 1
  • 9
  • 22
  • 1
    @Johannes Kuhn: Please do not mark it as duplicate. – Bishwash Mar 05 '18 at 05:33
  • 2
    Same as https://stackoverflow.com/questions/49103762/java-lang-nullpointerexception-on-myfirebaseinstanceidservice-only-some-times – Henry Mar 05 '18 at 05:36
  • I do not know which line might be causing the NullPointerException. NullPointerException is not appearing on my device. So i could not get the line causing problem. This is not a simple integer or string variable problem but related to MyFirebaseInstanceIDService which has different mechanism which i do not know in depth. – Bishwash Mar 05 '18 at 05:49

2 Answers2

0

If you want to resolve the issue with try/catch then I would suggest you catch all the exceptions which can be generated from the block.

try {
   response = client.newCall(request).execute();
   // System.out.println("working...");
} catch (Exception e) {
    e.printStackTrace();
} finally {
    response.body().close();
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • Do you mean to add if (response!=null){response.body().close();} try catch on it? – Bishwash Mar 05 '18 at 05:54
  • When the whole thing is in the try-catch block, you do not have to check them specifically. I saw the error is occurring in the `registerToken` function. So you need to put your try-catch smartly there. Any exception thrown from here can be found in the catch section of the block if you wrap them all with try catch. – Reaz Murshed Mar 05 '18 at 05:56
0

you have added response = client.newCall(request).execute(); in try catch block if there is any exception the response is null and it get crash in finally block for response.body().close();

 private void registerToken(String token) {
            OkHttpClient client = new OkHttpClient();
            RequestBody body = new FormBody.Builder()
                    .add("Token",token)
                    .build();

            Request request = new Request.Builder()
                    .url("http://example.com/register.php")
                    .post(body)
                    .build();

            try {

               response = client.newCall(request).execute();
               // System.out.println("working...");

            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(response!=null)
                response.body().close();
            }

        }
MJM
  • 5,119
  • 5
  • 27
  • 53
  • This could be the problem. Thanks for the suggestion. Do you see any possibilities of problem inside onTokenRefresh() ? – Bishwash Mar 05 '18 at 05:57
  • @Bishwash i think,other things are perfect – MJM Mar 05 '18 at 06:54
  • Do you think in the above code the networking operations is carried out on main thread, which should not be? Should i move code to make it async operation? – Bishwash Mar 05 '18 at 07:21