Martin Vandzura
Martin Vandzura

Reputation: 3127

ConnectivityManager getActiveNetworkInfo null pointer exception

I see occasionally null pointer exception in connectivity manager. From intent service I check network state by isOnWIFI(this). Exception occurs at line cm.getActiveNetworkInfo(). It's strange because I check for null before I call this. Note: Permissions are set.

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 happen? I can't debug it because it's only occasional from bugs reports. Only solution I see is to put it into try catch block. Thanks.

Upvotes: 11

Views: 20375

Answers (6)

kevthanewversi
kevthanewversi

Reputation: 3756

getActiveNetworkInfo returns null if no there's no default connection available as per the docs >> getActiveNetworkInfo .So what I'd advise you to do is check for the networks, each individually as 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    
}

Upvotes: 3

user3597165
user3597165

Reputation: 199

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() a second time, android goes and fetches the Network info a second time, only this time it does not encounters an active network returning null this second time. The thing is, not because you called getNetworkInfo() a millisecond before and didn´t get a null, means you are not going to get it a millisecond after

Upvotes: 6

Miguel Q
Miguel Q

Reputation: 3618

When i was working with services, this solved my issue.. Before i was just using !info.isConnected() but that was not enough

 ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();

        if(info==null || !info.isConnected()) {
            stopSelf();
            return;

        }

Upvotes: 0

Vinko Rajic
Vinko Rajic

Reputation: 1

I just made one template with this kind of problem , now it works good. You can try this code , I tried it on many devices and there is no problem. getActiveNetworkInfo() problem . Before I tried getAllNetworkInfo() and I do not understand what is wrong but getAllNetworkInfo() is working stupid on some devices.

Upvotes: -1

android_dev
android_dev

Reputation: 1477

are you Checked the api in that they are mentioned if there are no active connections they are simply returning null i think this is ur problem.

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

Upvotes: 6

Haris Aftab
Haris Aftab

Reputation: 259

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

 }  
}

Upvotes: 3

Related Questions