ConnectivityManager getActiveNetworkInfo null pointer exception

Sometimes I see an exception exception in the pointer in the connection manager. From the Intent service, I check the status of the network using isOnWIFI (this). An exception occurs in the line cm.getActiveNetworkInfo (). This is strange because I am checking for null until I name it. Note. Permissions are installed.

public static boolean isOnWIFI(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); return cm != null //here occurs NullPointerException && cm.getActiveNetworkInfo() != null && ((cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI) != null && cm.getNetworkInfo( ConnectivityManager.TYPE_WIFI).isConnected())); } 

Any thoughts why this is happening? I cannot debug it because these are just random error messages. The only solution I see is to put it in a catch try block. Thanks.

+7
source share
6 answers

You checked api that they are mentioned if there are no active connections which they simply return null. I think this is a problem with ur.

http://developer.android.com/reference/android/net/ConnectivityManager.html #getActiveNetworkInfo ()

+6
source

android_dev is right. The problem in your code comes from cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()));

Yes! you check cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI) != null before, but by calling getNetworkInfo() second time, the android receives and retrieves network information a second time, but only this time it does not encounter an active network returning zero this second time. Itโ€™s not that you called getNetworkInfo() millisecond earlier and did not get zero, which means you wonโ€™t get milliseconds after

+5
source
 Try this. Read more here 

http://developer.android.com/training/basics/network-ops/managing.html

 public static boolean isOnWIFI(Context context) { ConnectivityManager cm = (ConnectivityManager) this.getSystemService(context.CONNECTIVITY_SERVICE); if(cm != null){ NetworkInfo networkInfo = cm.getActiveNetworkInfo(); boolean isWiFi = networkInfo.getType() == ConnectivityManager.TYPE_WIFI; // if user is connected to network if (isWifi) { return true; }else { return false; } } else{ //cm is null return false } } 
+2
source

When I worked with services, this solved my problem. Before I just used it! info.isConnected (), but that wasnโ€™t enough

  ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); NetworkInfo info = cm.getActiveNetworkInfo(); if(info==null || !info.isConnected()) { stopSelf(); return; } 
0
source

getActiveNetworkInfo returns null if there is no default connection available according to documents - > getActiveNetworkInfo . What I would advise you to do is check the networks, each individually, as shown below.

 NetworkInfo WiFiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); NetworkInfo mobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 

then check if they are available

 if(mobileInfo != null & mobileInfo.isConnectedOrConnecting() || WiFiInfo != null & WiFiInfo.isConnectedOrConnecting()) { //do your task here } else { //show an alert dialog or something } 
0
source

I just created one template with such a problem, now it works well. You can try this code, I tried it on many devices and there are no problems. getActiveNetworkInfo() . Before I tried getAllNetworkInfo() , and I do not understand what is wrong, but getAllNetworkInfo() works stupidly on some devices.

-one
source

All Articles