Android Question Handle MQTT connection problem when no internet connection

lsahidin

Member
Hello,
I use B4A with mqtt library and try to build connect and reconnect with code from this tutorial.
https://www.b4x.com/android/forum/threads/b4x-mqtt-connect-reconnect.80815/
everything is fine, but after I try to disable internet connection on my phone error occurs. I change the code with try catch but no luck, then I read some forum they suggest to update version to mqttv3:1.1.1, is it true? hope somebody could help to continue my learning with B4A. If any newer library version, where I can download? I attached the error message, thanks.

B4X:
Error connecting.
Trying to connect
Error connecting.
Trying to connect
Error occurred on line: 35 (Starter)
java.lang.NullPointerException: Attempt to invoke virtual method 'org.eclipse.paho.client.mqttv3.IMqttToken org.eclipse.paho.client.mqttv3.MqttAsyncClient.connect(org.eclipse.paho.client.mqttv3.MqttConnectOptions, java.lang.Object, org.eclipse.paho.client.mqttv3.IMqttActionListener)' on a null object reference
    at anywheresoftware.b4j.objects.MqttAsyncClientWrapper.Connect2(MqttAsyncClientWrapper.java:83)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:22)
    at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1705)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6746)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 

lsahidin

Member
Does it happen in Release mode? Can you post the logs in release?

Hi Erel, thanks for your reply, here is the logs in release:
logs in release:
Logger connected to:  HMD Global Nokia 4.2
--------- beginning of main
*** Service (starter) Create ***
Trying to connect
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Mqtt connected
** Activity (main) Pause, UserClosed = false **
** Service (starter) Destroy (ignored)**
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
*** Service (starter) Create ***
Trying to connect
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Mqtt connected
Disconnected
Trying to connect
Error connecting.
Trying to connect
Error connecting.
Trying to connect
--------- beginning of crash
java.lang.RuntimeException: java.lang.NullPointerException: Attempt to invoke virtual method 'org.eclipse.paho.client.mqttv3.IMqttToken org.eclipse.paho.client.mqttv3.MqttAsyncClient.connect(org.eclipse.paho.client.mqttv3.MqttConnectOptions, java.lang.Object, org.eclipse.paho.client.mqttv3.IMqttActionListener)' on a null object reference
    at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1707)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6746)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'org.eclipse.paho.client.mqttv3.IMqttToken org.eclipse.paho.client.mqttv3.MqttAsyncClient.connect(org.eclipse.paho.client.mqttv3.MqttConnectOptions, java.lang.Object, org.eclipse.paho.client.mqttv3.IMqttActionListener)' on a null object reference
    at anywheresoftware.b4j.objects.MqttAsyncClientWrapper.Connect2(MqttAsyncClientWrapper.java:83)
    at b4a.example.starter$ResumableSub_ConnectAndReconnect.resume(starter.java:209)
    at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1705)
    ... 7 more
 
Upvote 0

lsahidin

Member
Thanks! I have changed the code. before MqttClient was initialized it will not calling Conncet2 and continue process then back to top. If internet connection available it will trying to connect again.

Original code:
original code:
Sub ConnectAndReconnect
   Do While working
     If mqtt.IsInitialized Then mqtt.Close
     mqtt.Initialize("mqtt", "ssl://io.adafruit.com:8883", "B4X" & Rnd(0, 999999999))
     Dim mo As MqttConnectOptions
     mo.Initialize(username, password)
     Log("Trying to connect")
     mqtt.Connect2(mo)
     Wait For Mqtt_Connected (Success As Boolean)
     If Success Then
       Log("Mqtt connected")
       Do While working And mqtt.Connected
         mqtt.Publish2("ping", Array As Byte(0), 1, False) 'change the ping topic as needed
         Sleep(5000)
       Loop
       Log("Disconnected")
     Else
       Log("Error connecting.")
     End If
     Sleep(5000)
   Loop
End Sub

Code after modification:
code after:
Sub ConnectAndReconnect
   Do While working
     If mqtt.IsInitialized Then mqtt.Close
     mqtt.Initialize("mqtt", "ssl://io.adafruit.com:8883", "B4X" & Rnd(0, 999999999))
     Dim mo As MqttConnectOptions
     mo.Initialize(username, password)
     Log("Trying to connect")
     If mqtt.IsInitialized Then
         mqtt.Connect2(mo)
         Wait For Mqtt_Connected (Success As Boolean)
         If Success Then
            Log("Mqtt connected")
            Do While working And mqtt.Connected
                mqtt.Publish2("ping", Array As Byte(48), 1, False) 'change the ping topic as needed, byte 48 for 0 in ascii
                Sleep(5000)
            Loop
            Log("Disconnected")
         End If
     Else
       mqtt.Close
       Log("Error connecting.")
     End If
     Sleep(5000)
   Loop
End Sub
 
Last edited:
Upvote 0
Top