Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[paho-dev] Android Service Exception

Hello,
I have found a rare occurring exception in Paho Android Service. 

1) Here is the exception details:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.net.NetworkInfo.isAvailable()' on a null object reference
	at org.eclipse.paho.android.service.MqttService.isOnline(MqttService.java:814)
	at org.eclipse.paho.android.service.MqttService$NetworkConnectionIntentReceiver.onReceive(MqttService.java:795)
	at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:866)
	... 8 more


2) This exception was discovered on a device which has frequent network changes between 3G and 4G. For testing purpose, I could repro this issue simply by turning on and off airplane mode every 30 seconds.


3) Here is the original code, where exception occurred:

public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
if (cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()
&& backgroundDataEnabled) {
return true;
}

return false;
}


4) Here is my modified code, which fixes the issue:

public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

if (cm != null){
NetworkInfo networkInfo = cm.getActiveNetworkInfo();

if(networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected() && backgroundDataEnabled) {
return true;
}
}

return false;
}


5) I will be happy to submit the modified code to the develop branch of Paho Android Service. Please let know if this is acceptable.


Thanks,
Kamaljeet Maini

Back to the top