0

at the start of my application (In the MainActivity onCreate) I ask if there is an internet connection with following code:

boolean connected = false;
ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
        connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
    //we are connected to a network
    connected = true;
} else
    connected = false;

but I get following crash report in my Google Dev console from mixed android versions:

java.lang.RuntimeException: 

  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2944)

  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3079)

  at android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:78)

  at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:108)

  at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:68)

  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1836)

  at android.os.Handler.dispatchMessage (Handler.java:106)

  at android.os.Looper.loop (Looper.java:193)

  at android.app.ActivityThread.main (ActivityThread.java:6702)

  at java.lang.reflect.Method.invoke (Native Method)

  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)

  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:911)
Caused by: java.lang.NullPointerException: 

  at com.fujigames.memesoundboard.MainActivity.onCreate (MainActivity.java:99)

  at android.app.Activity.performCreate (Activity.java:7136)

  at android.app.Activity.performCreate (Activity.java:7127)

  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1271)

  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2924)

MainActivity.java:99 is the code I have shown above. Why am I getting this crash and what should I do to fix it? Thanks.

user207421
  • 305,947
  • 44
  • 307
  • 483
Aaron Waller
  • 175
  • 4
  • 21

1 Answers1

0

Why am I getting this crash?

  1. The getSystemService call can return a null. You need to check for this and deal with it.

  2. The getNetworkInfo call can return a null. You need to check for this and deal with it.

You are not checking for null returns, so it looks like one of these is the root cause of your NPEs.


What should I do to fix it?

To solve these problems, first read the javadocs for the respective methods to understand when / why a null could be returned. Then modify your code to do the appropriate thing.

(Figure out what should your app should do if there is not networking service at all, or if Wifi and/or Mobile were unavailable? We can't tell you that.)


Note that the javadocs say that getNetworkInfo(...) is deprecated in API level 29. So this code may need to be completely replaced at some point in the future.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Okay but why does it work on 97% of the users devices, only 3% of my users are getting this NullPointerException. – Aaron Waller Jan 24 '20 at 02:46
  • I can't answer that. Apart from the obvious ... something is different on / about the 3% users' devices. It is moot if you write your code to be more robust; i.e. test for `null` in the cases where the javadocs state that `null` may be returned! – Stephen C Jan 24 '20 at 02:59