in my App, i connecto to MQTT server/broker, the connection seetings/configuration is retrieved from SQLiteDB. I checked the entries retrieved (ip, port, clientID, KAtimer, sessionFlag) and all of them valid.
These configuration is returned in onActivityResult
, and in onActivityResult
, i call the method
private void setUpMQTTEnvironment(Bundle extras)
it is not posted below, it is jus to extract the values from the bundle returned from the subActivity and assign each value contained in the bundle to its corresponding set() method
, for an example, when i extract the IP and the PORT i assin them to setIP(ip) and setPORT(port)
in onResume()
i call MQTTConnect(...,..)
posted below to connect to the server, Actually tp to this step the synchronous callback of the connect
method say connection fails
but this also mean that the client object is not null
the problem is when i ress the back button, onpause
is called and I receive NPE
as shown in the logcat errors
why i receive NPE
? i checked if the clien object is null or not and if it is not null i should not recive NPE
error and i it is null the App should close normally.
please help me to find the error, why i receive NPE
MQTTConnect:
private void MQTTConnect(MqttAndroidClient client, MqttConnectOptions opts) throws MqttException {
// TODO Auto-generated method stub
client.registerResources(this);
client.setCallback(asynchCallBack);
client.connect(opts, getApplicationContext(), synchCallBack);
this.setClient(client);
}
onResume:
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.w(TAG, "@onResume()");
if ( (this.MQTT_ASSETS_AVAILABLE) && (this.getClient() != null) && (this.getClientOpts() != null) ) {
Log.d(TAG, "@onResume(): ConnectionAssets: not null, Client: not null, opts: not null");
try {
MQTTConnect(this.getClient(), this.getClientOpts());
} catch (MqttException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e(TAG, "@onResume: Exception catched -> Connection problems.");
}
} else {
Log.d(TAG, "@onResume(): either ConnectionAssets are not available or client/opts is null");
}
}
onPause:
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.w(TAG, "@onPause()");
if ((this.getClient() != null) ) {
if (this.getClient().isConnected()) {
Log.w(TAG, "@onPause(): client is not null");
this.getClient().close();
this.getClient().unregisterResources();
try {
this.getClient().disconnect(this, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken arg0) {
// TODO Auto-generated method stub
Log.w(TAG, "@onSuccess(): disconnection successfull");
}
@Override
public void onFailure(IMqttToken arg0, Throwable arg1) {
// TODO Auto-generated method stub
Log.w(TAG, "@onFailure(): disconnection failed");
}
});
} catch (MqttException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
LogCat Error:
02-04 12:32:37.618: E/AndroidRuntime(10392): FATAL EXCEPTION: main
02-04 12:32:37.618: E/AndroidRuntime(10392): Process: com.example.mqtt_designlayout_02, PID: 10392
02-04 12:32:37.618: E/AndroidRuntime(10392): java.lang.RuntimeException: Unable to pause activity {com.example.mqtt_designlayout_02/com.example.mqtt_designlayout_02.MainActivity}: java.lang.NullPointerException
02-04 12:32:37.618: E/AndroidRuntime(10392): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3185)
02-04 12:32:37.618: E/AndroidRuntime(10392): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3140)
02-04 12:32:37.618: E/AndroidRuntime(10392): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3118)
02-04 12:32:37.618: E/AndroidRuntime(10392): at android.app.ActivityThread.access$1000(ActivityThread.java:157)
02-04 12:32:37.618: E/AndroidRuntime(10392): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1264)
02-04 12:32:37.618: E/AndroidRuntime(10392): at android.os.Handler.dispatchMessage(Handler.java:102)
02-04 12:32:37.618: E/AndroidRuntime(10392): at android.os.Looper.loop(Looper.java:157)
02-04 12:32:37.618: E/AndroidRuntime(10392): at android.app.ActivityThread.main(ActivityThread.java:5293)
02-04 12:32:37.618: E/AndroidRuntime(10392): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 12:32:37.618: E/AndroidRuntime(10392): at java.lang.reflect.Method.invoke(Method.java:515)
02-04 12:32:37.618: E/AndroidRuntime(10392): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
02-04 12:32:37.618: E/AndroidRuntime(10392): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
02-04 12:32:37.618: E/AndroidRuntime(10392): at dalvik.system.NativeStart.main(Native Method)
02-04 12:32:37.618: E/AndroidRuntime(10392): Caused by: java.lang.NullPointerException
02-04 12:32:37.618: E/AndroidRuntime(10392): at org.eclipse.paho.android.service.MqttAndroidClient.isConnected(MqttAndroidClient.java:227)
02-04 12:32:37.618: E/AndroidRuntime(10392): at com.example.mqtt_designlayout_02.MainActivity.onPause(MainActivity.java:513)
02-04 12:32:37.618: E/AndroidRuntime(10392): at android.app.Activity.performPause(Activity.java:5493)
02-04 12:32:37.618: E/AndroidRuntime(10392): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1251)
02-04 12:32:37.618: E/AndroidRuntime(10392): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3171)