Android Question MQTT Subscribe problem with reconnect sample. Null object reference

josejad

Expert
Licensed User
Longtime User
Hi there¡¡

EDITED: The error is easiliest reproduced if you call ConnectAndReconnect from B4XPage_Created instead of B4XPage_Appear. If not, the error just happens from time to time.
EDITED 2: A quick way of getting the error. Disconnect wifi, when it reconnects througth mobile connection, the error happens

I'm using Erel's sample Connect and Reconnect sample in a B4XPages project.
My problem is subscribing to a Topic.

B4X:
Sub ConnectAndReconnect
    Do While working
        If mqtt.IsInitialized Then mqtt.Close
        mqtt.Initialize("mqtt", "tcp://test.mosquitto.org", "B4X" & Rnd(0, 999999999))
        'Dim mo As MqttConnectOptions
        'mo.Initialize(username, password)
        Log("Trying to connect")
        'mqtt.Connect2(mo)
        mqtt.Connect
        Wait For mqtt_Connected (Success As Boolean)
        If Success Then
            Log(mqtt.IsInitialized)               '<-Added¡¡¡¡ The log is TRUE
            mqtt.Subscribe("B4XTest", 1)    '<-Added¡¡¡¡ Error MqttAsyncClient.subscribe(java.lang.String, int)' on a null object reference
            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

From time to time you get this error:

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
Trying to connect
Mqtt connected
** Activity (main) Pause event (activity is not paused). **
** Activity (main) Resume **
Trying to connect
Disconnected
Trying to connect
True
Error occurred on line: 52 (B4XMainPage)
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:116)
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:193)
at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:22)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:137)
at anywheresoftware.b4a.BA$2.run(BA.java:387)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8587)
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)

Sometimes the error is "The client is not connected", despite it happens when mqtt_Connected is Success.

See attached a project with the problem.
EDITED: To "accelerate" the error, change the call to ConnectAndReconnect from B4XPage_Created to B4XPage_Appear
Just open the app, press home button and go back to the app one or two times.



Thanks in advance
 

Attachments

  • MQTTTest.zip
    14.3 KB · Views: 226
Last edited:

josejad

Expert
Licensed User
Longtime User
1. Add Log("ConnectAndReconnect") to the top of that sub.
Done

Call it from B4XPage_Created.
Done.

Now I run the app, and change the way it's connected (i.e. connected with wifi, then disconnect wifi and wait the mobile data try to reconnect). Connected with USB debug
B4X:
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
*** mainpage: B4XPage_Created
ConnectAndReconnect
Trying to connect
*** mainpage: B4XPage_Appear
** Activity (main) Resume **
true
Mqtt connected
Disconnected mqtt
Disconnected
Trying to connect
false
Error occurred on line: 59 (B4XMainPage)             '<---------------------------- mqtt.Subscribe("B4XTest", 1)
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:116)
    at b4a.example.b4xmainpage$ResumableSub_ConnectAndReconnect.resume(b4xmainpage.java:197)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    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:193)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:137)
    at anywheresoftware.b4a.BA$2.run(BA.java:387)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:246)
    at android.app.ActivityThread.main(ActivityThread.java:8587)
    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)
** Activity (main) Pause event (activity is not paused). **

B4X:
Registro conectado a: RZ8N11K70XX
--------- beginning of crash
--------- beginning of system
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
*** mainpage: B4XPage_Created
ConnectAndReconnect
Trying to connect
*** mainpage: B4XPage_Appear
** Activity (main) Resume **
true
Mqtt connected
Disconnected mqtt
Disconnected
Trying to connect
false
b4xmainpage$ResumableSub_ConnectAndReconnectresume (java line: 181)
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:116)
    at b4a.example.b4xmainpage$ResumableSub_ConnectAndReconnect.resume(b4xmainpage.java:181)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:207)
    at anywheresoftware.b4a.BA$2.run(BA.java:387)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:246)
    at android.app.ActivityThread.main(ActivityThread.java:8587)
    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)
 

Attachments

  • MQTTTest.zip
    14.3 KB · Views: 234
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try it with this code:
B4X:
Sub ConnectAndReconnect
    Log("ConnectAndReconnect")
    Do While working
        If mqtt.IsInitialized Then mqtt.Close
        Do While mqtt.IsInitialized
            Sleep(100)
        Loop
        mqtt.Initialize("mqtt", "tcp://test.mosquitto.org", "B4X" & Rnd(0, 999999999))
        'Dim mo As MqttConnectOptions
        'mo.Initialize(username, password)
        Log("Trying to connect")
        'mqtt.Connect2(mo)
        mqtt.Connect
        Wait For mqtt_Connected (Success As Boolean)
        If Success Then
            Log(mqtt.IsInitialized)
            mqtt.Subscribe("B4XTest", 1)
            Button1.Color = xui.Color_Green
            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
            Button1.Color = xui.Color_Red
            Log("Disconnected")
        Else
            Log("Error connecting.")
        End If
        Sleep(5000)
    Loop
End Sub
 
Upvote 1

josejad

Expert
Licensed User
Longtime User
Thanks¡¡ It seems to avoid the error. I will test for a few days¡¡

B4X:
        Do While mqtt.IsInitialized
            Sleep(100)
        Loop
 
Upvote 0

Gabino A. de la Gala

Active Member
Licensed User
Longtime User
I think it is something that has changed between version 1.00 and 1.01.

With 1.00 it never "breaks" me and yet the same code with version 1.01 always ...

This example is compiled with version 1.00 of jMQTT.
 

Attachments

  • TestMQTT.apk
    238.6 KB · Views: 208
Upvote 0

Lakhtin_V

Active Member
Licensed User
Longtime User
Thanks¡¡ It seems to avoid the error. I will test for a few days¡¡

B4X:
        Do While mqtt.IsInitialized
            Sleep(100)
        Loop
Try it with this code:
B4X:
Sub ConnectAndReconnect
    Log("ConnectAndReconnect")
    Do While working
        If mqtt.IsInitialized Then mqtt.Close
        Do While mqtt.IsInitialized
            Sleep(100)
        Loop
        mqtt.Initialize("mqtt", "tcp://test.mosquitto.org", "B4X" & Rnd(0, 999999999))
        'Dim mo As MqttConnectOptions
        'mo.Initialize(username, password)
        Log("Trying to connect")
        'mqtt.Connect2(mo)
        mqtt.Connect
        Wait For mqtt_Connected (Success As Boolean)
        If Success Then
            Log(mqtt.IsInitialized)
            mqtt.Subscribe("B4XTest", 1)
            Button1.Color = xui.Color_Green
            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
            Button1.Color = xui.Color_Red
            Log("Disconnected")
        Else
            Log("Error connecting.")
        End If
        Sleep(5000)
    Loop
End Sub
I want to know the time it took for a signal to reach a specific server. How can I use this library for this or maybe you know other ways to get the ping time.
 
Upvote 0
Top