paho.mqtt.android icon indicating copy to clipboard operation
paho.mqtt.android copied to clipboard

Some time I got this error java.lang.NullPointerException: Attempt to invoke virtual method 'void org.eclipse.paho.android.service.MqttService.subscribe(java.lang.String, java.lang.String, int, java.lang.String, java.lang.String)' on a null object reference

Open rajeshkanna777 opened this issue 7 years ago • 7 comments

Please fill out the form below before submitting, thank you!

  • [ ] Bug exists Release Version 1.1.1 (Java Repository Master Branch)
  • [ ] Bug exists in Snapshot Version 1.1.2-SNAPSHOT (Android Service Repository Master Branch)
  • [ ] Bug is just in the Sample Application.

Android API Version Bug Seen on:

Android Version Bug Seen on:

Please also check that if you have found the bug in the Release version (1.1.1) that you check that it also exists in the Snapshot (1.1.2-SNAPSHOT) before raising a bug.

Description of Bug:

E.g. Steps to re-create, how often does this happen etc..

Console Log output (if available):

rajeshkanna777 avatar Sep 07 '17 09:09 rajeshkanna777

This might happen because you are subscribing before connecting to your server. Subscribe after you successfully connect to your server.

bzrampurawala avatar Sep 24 '17 21:09 bzrampurawala

But am using this paho.mqtt.android.example app so i didnt see subscription happen before connecting to the server, and this code is working mqtt3.1.1.0 version connection lost happen only mqtt3.1.1.1

rajeshkanna777 avatar Nov 29 '17 01:11 rajeshkanna777

Got the same error here. Is there a way to to check if it is connected or not before subscribing? I've tried connecting and then waiting 10 seconds and then subscribing but it doesn't work so I suspect I am unable to connect properly.

antgustech avatar Dec 12 '17 07:12 antgustech

You can use isConnected() from the class MqttAndroidClient

I got that error too and if I call the function above it obviously returns false

ghost avatar Dec 26 '17 23:12 ghost

I'm having this error thrown and I have no idea how it is possible. I cannot reproduce it myself. this is my code:

if (doReconnect && mqttAndroidClient != null && mqttAndroidClient.isConnected()) {
     mqttAndroidClient.subscribe(topic, 0, null, new IMqttActionListener() {
         @Override
         public void onSuccess(IMqttToken asyncActionToken) {
             Timber.d("Subscribed to %s!", topic);
         }
         @Override
         public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
             Timber.d("Failed to subscribe to %s", topic);
         }
     });

The mqttAndroidClient.isConnected()) should return false and should avoid this issue.

This is the stacktrace:

       at org.eclipse.paho.android.service.MqttAndroidClient.subscribe(MqttAndroidClient.java:909)
       at com.<removed>.mqtt.Mqtt.subscribeToTopic(Mqtt.java:184)

line 184 is the call to mqttAndroidClient.subscribe()

boylenssen avatar Nov 21 '20 10:11 boylenssen

I'm having the same issue.

    private fun subscribeToTopic() {
        val subscriptionTopic = sharedPref.getString(getString(R.string.PREF_MQTT_TOPIC), "#").toString()

        try {
            mqttAndroidClient?.subscribe(subscriptionTopic, 0, null, object : IMqttActionListener {
                override fun onSuccess(asyncActionToken: IMqttToken) {
                    Log.w("Mqtt", "Subscribed!")
                    setMQTTStatusMessage("MQTT connected")
                }

                override fun onFailure(asyncActionToken: IMqttToken, exception: Throwable) {
                    Log.w("Mqtt", "Subscribed fail!")
                    setMQTTStatusMessage("MQTT subscribe failed - check your keys")

                    selfStop = true
                    stopLocationTracking()
                    AppAggregate.stopService()
                }
            })

        } catch (ex: MqttException) {
            ex.printStackTrace()
        } catch (ex: IllegalArgumentException) {
            ex.printStackTrace()
        }
    }

Crash dump

java.lang.NullPointerException: 
  at org.ttnmapper.phonesurveyor.services.MyService$subscribeToTopic$1.onFailure (Unknown Source:7)
  at org.eclipse.paho.android.service.MqttTokenAndroid.notifyFailure (MqttTokenAndroid.java:146)
  at org.eclipse.paho.android.service.MqttAndroidClient.simpleAction (MqttAndroidClient.java:1501)
  at org.eclipse.paho.android.service.MqttAndroidClient.subscribeAction (MqttAndroidClient.java:1526)
  at org.eclipse.paho.android.service.MqttAndroidClient.onReceive (MqttAndroidClient.java:1377)
  at androidx.localbroadcastmanager.content.LocalBroadcastManager.executePendingBroadcasts (LocalBroadcastManager.java:313)
  at androidx.localbroadcastmanager.content.LocalBroadcastManager$1.handleMessage (LocalBroadcastManager.java:121)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:246)
  at android.app.ActivityThread.main (ActivityThread.java:8633)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:602)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1130)

I do not understand why I am getting a NPE in the onFailure callback.

jpmeijers avatar Dec 10 '21 10:12 jpmeijers

Hi everyone, i don't know if you were able to solve this issue after many time. I am facing the same issue and I have noticed that this happens when the clients results connect but can't subscribe due to poor band. I have tried to reproduce the error but with no luck. Did you solved it after so long? Is fo, how? Thank you very much.

augelloantonio avatar Nov 28 '22 10:11 augelloantonio