0

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)
rmaik
  • 1,076
  • 3
  • 15
  • 48
  • why are you making a network request in onpause? – Illegal Argument Feb 04 '15 at 11:56
  • @Blackbelt isConnected() is abuilt in method in paho library i imported in my project. i have not developed isConnected()!! – rmaik Feb 04 '15 at 11:56
  • Put a breakpoint on the first line of the method and step through the debugger. You can examine everything to see if it's null or not. No need to rely on guessing! – Simon Feb 04 '15 at 11:57
  • @IllegalArgument no, i am not requesting connection in onPause, i connect in onResum and if the (clientobject !=null) && (clientObj.isconnected()) i disconnect the client in onPause – rmaik Feb 04 '15 at 11:57
  • @Simon i have not used debugger before, but sure i would use it now. shoud i only set a break point and run the code? – rmaik Feb 04 '15 at 11:59
  • the NPE appears in `MqttAndroidClient.isConnected(MqttAndroidClient.java:227)` – Blackbelt Feb 04 '15 at 12:02
  • 1
    do you have he service tag " – Amrmsmb Feb 04 '15 at 13:01
  • Read this, https://developer.android.com/tools/debugging/debugging-studio.html, trying to build apps without the debugger is like trying to build a house without the tools you need. – Simon Feb 04 '15 at 14:23

0 Answers0