B4J Question jMQTT error, paho version.

Similar threads

B4J Library jMQTT - Official MQTT client
B4A Tutorial [B4X] MQTT Chat Room
B4J Tutorial [IoT] MQTT Protocol
B4A Code Snippet [B4X] MQTT SSL and Self Signed Certificates
B4J Library MqttBroker

pompierecattivo

Member
Licensed User
Hello,
sorry for my bad english and also for my basic knowledge about code writing.
I've got a crash on my app using jMQTT library.
I've read by erel that the library haven't update since today because it's stable. In my case the error (quite difficult for me to intercept it) on debug window start whith

java.lang.NullPointerException: Attempt to invoke virtual method 'org.eclipse.paho.client.mqttv3.IMqttToken org.eclipse.paho.client.mqttv3.MqttAsyncClient.subscribe(java.lang.String, int)' on a null object reference

Searching on th web, i've found this interesting page:
https://github.com/eclipse/paho.mqtt.android/issues/208

In effect, i got th error when i try to close mqtt client by 'mqttclient.close' command on lost connections.

Again, i'm a 'sunday programmer', and i dont know alot of java, so i'm not always sure for all i do... But if my question could be useful also for other than me, i will glad to read replyes.
Thank you.

Maurizio.
 

pompierecattivo

Member
Licensed User
I tryed without luck to replicate error. But i'm sure sometime this happen.
Actually all i can do is paste the copy of log i doing when error last occurr. I didn't copy more of this, at those time:

java.lang.NullPointerException: Attempt to invoke virtual method 'org.eclipse.paho.client.mqttv3.IMqttToken org.eclipse.paho.client.mqttv3.MqttAsyncClient.subscribe(java.lang.String, int)' on a null object reference
at anywheresoftware.b4j.objects.MqttAsyncClientWrapper.Subscribe(MqttAsyncClientWrapper.java:91)
at com.rbsoft.sardroid.starter$ResumableSub_ConnectAndReconnect.resume(starter.java:937)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:245)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:185)
at anywheresoftware.b4a.BA$2.run(BA.java:365)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6130)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

I don't know if this can help you recognize something. I'm still try to force the crash. When i'll be able to do, will paste here all detail i can.
 

pompierecattivo

Member
Licensed User
I will check. The only subscribe point is inside your connect&reconnect sub. I paste my code..
B4X:
Sub ConnectAndReconnect
    Do While working
        If MQTTClient.IsInitialized Then MQTTClient.Close
        'setta main.porta per prevenire errori con l'avvio di Starter. Verrà modificata in seguito
        If Main.Porta = "" Then Main.Porta = "1883"
        MQTTClient.Initialize("mqtt", "tcp://" & Main.Server & ":" & Main.Porta, Main.IMEITelefono)
        Dim mo As MqttConnectOptions
        mo.Initialize("myID", "myPW")
        mo.CleanSession = True
        MQTTClient.Connect2(mo)
        Log ("C&R mqtt"& " - "& "tcp://" & Main.Server & ":" & Main.Porta & " - " & Main.IMEITelefono)
        Log("Trying to connect")
        Wait For mqtt_Connected (Success As Boolean)
        If Success Then
            Log("Mqtt connected (working=" & working &")")
            Connesso = True
            MQTTClient.Subscribe( "ahaConn/" & Main.KEY & "/SardroidVersusClients", 0)
            Do While working And MQTTClient.Connected
                Dim pingstring As String
                pingstring = "@" & Main.NAM & ";" & Main.Versione & ";" & Main.IMEITelefono
                MQTTClient.Publish2("ahaConn/" & Main.KEY & "/Sardroidping",  pingstring.GetBytes("UTF8"), 0, False) 'change the ping topic as needed
                Sleep(5000)
            Loop
            Log("Disconnected")
            Connesso = False
        Else
            Log("Error connecting.")
            Connesso = False
        End If
        Sleep(5000)
    Loop
End Sub

based on this, i've thinked there's no chance to subscribe if client isn't initialized...

will report any news in days.
thank you!
 

pompierecattivo

Member
Licensed User
I've check for multiple ConnectAndReconnect call istances and I haven't found. I've little changed my code and now, when i ask for close connection i receive this:

java.lang.NullPointerException: Attempt to invoke virtual method 'void org.eclipse.paho.client.mqttv3.internal.CommsTokenStore.quiesce(org.eclipse.paho.client.mqttv3.MqttException)' on a null object reference
at org.eclipse.paho.client.mqttv3.internal.ClientComms.shutdownConnection(ClientComms.java:310)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:608)
at java.lang.Thread.run(Thread.java:761)

and the app crash.

It's seems to be different from other one, but i'm not sure by myself about all java environmnt...

I'm using app in relase mode, because i'm not able to repeat error in debug mode (but i'm still testing)
 
Last edited:

pompierecattivo

Member
Licensed User
Some news: not always i ask for 'mqttclient.close' my app crash. Sometime just "freeze" for some seconds (30-40), then run again correctly.
To be more precise... i need to close my connection because i need, sometime, to change address of MQTT broker (actually using mosquitto in locals or iot.eclipse.org for generic tests)
 

pompierecattivo

Member
Licensed User
Hello Erel an all, sorry for anwer delay.
I understand what you've suggest. But i've tryed another way that appear to run...
I've put another flag on connect and reconnect sub and when this flag (MQTTok) if 'false', sub redo connection with new paramenters.

Here's the code (again, please, dont judge my code. I'm a bad programmer...)

B4X:
        If Success Then
            Log("Mqtt connected (working=" & working &")")
            Connesso = True
            MQTTok = True
            If MQTTClient.IsInitialized = False Then Return
            MQTTClient.Subscribe( "ahaConn/" & KEY & "/SardroidVersusClients", 0)
            Do While working And MQTTClient.Connected And MQTTok
                Dim pingstring As String
                pingstring = IMEITelefono & ";" &  NAM & ";" & Versione & ";" & SendGPS.TeleStatus& ";" & TEL
                'cripta messaggio
                CryptPing = AES_Encrypt(pingstring,"abcdefg_abcdefg_","sardroid_di_pompiere_cattivo_chi")
                MQTTClient.Publish2("ahaConn/" & KEY & "/Sardroidping",  CryptPing.GetBytes("UTF8"), 0, False) 'change the ping topic as needed
                Sleep(5000)
            Loop
            Log("Disconnected")
            Connesso = False
        Else
            Log("Error connecting.")
            Connesso = False
        End If
 
Top