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

java.lang.NullPointerException: Attempt to invoke virtual method 'long org.eclipse.paho.client.mqttv3.internal.ClientState.getKeepAlive()' on a null object reference

Open hoangchungk53qx1 opened this issue 2 years ago • 4 comments

Screen Shot 2022-07-30 at 19 40 39

hoangchungk53qx1 avatar Jul 30 '22 12:07 hoangchungk53qx1

No clue about your implementation, but you miss call of override fun init(comms: ClientComms)

hannesa2 avatar Jul 30 '22 20:07 hannesa2

I implemented it like this

 fun connectMqttClient(
       context: Context,
       userId: String?,
       flexId: String? = ""
   ): Flow<Pair<Boolean, MqttAndroidClient>> = callbackFlow {
       mqttClient = MqttAndroidClient(
           context,
           BuildConfig.MQTT_DOMAIN,
           "android_${userId}_$deviceId${MqttClient.generateClientId()}"
       )
       mqttClient.setCallback(object : MqttCallback {
           override fun messageArrived(topic: String?, message: MqttMessage?) {
               Timber.tag(TAG).d("messageArrived  ${message.toString()}")
               busEvent(MessageMqtt(message = message.toString(), topic = topic))
           }

           override fun connectionLost(cause: Throwable?) {
               mqttClient.reconnect()
               Timber.tag(TAG).d("Connection lost ${cause.toString()}")
           }

           override fun deliveryComplete(token: IMqttDeliveryToken?) {

           }
       })
       val options = MqttConnectOptions()
       options.isAutomaticReconnect = true

       try {
           mqttClient.connect(options, null, object : IMqttActionListener {
               override fun onSuccess(asyncActionToken: IMqttToken?) {
                   trySend(Pair(true, mqttClient))
               }

               override fun onFailure(asyncActionToken: IMqttToken?, exception: Throwable?) {
                   trySend(Pair(false, mqttClient))
               }
           })
       } catch (e: MqttException) {
           e.printStackTrace()
       }
       awaitClose { mqttClient.connect() }
   }

hoangchungk53qx1 avatar Jul 31 '22 04:07 hoangchungk53qx1

This extendedSample works properly, and maybe can be improved on several ways. But this is not the issue.

if you have the running code, it should not be very difficult, to debug your code. When you found an issue, please show me how to improve

hannesa2 avatar Jul 31 '22 05:07 hannesa2

Or you can make a pull request eg. in basicSample with your implementation and demonstrate what's going wrong. I would love to see here flow in action. But with a code snippet alone, I can't do nothing

hannesa2 avatar Jul 31 '22 05:07 hannesa2

Do you have any news? i'm facing the same issue.

lotaviods avatar Dec 01 '22 15:12 lotaviods

Do you have any news? i'm facing the same issue.

But i'm not able to recreate the scenario, only getting this in firebase crashlytics

lotaviods avatar Dec 01 '22 16:12 lotaviods

The label A sample is required is still valid

hannesa2 avatar Dec 02 '22 05:12 hannesa2

Without running sample I close it. You can reopen it if you still need it

hannesa2 avatar Aug 09 '23 04:08 hannesa2

No clue about your implementation, but you miss call of override fun init(comms: ClientComms)

Can you please elaborate on "you missed call of init(comms)"? Were should we init it?

I tried reproducing it, but couldnt but we are getting crashlytics data hence trying to understand what might have gone wrong.

kashyap-5paisa avatar Apr 10 '24 04:04 kashyap-5paisa