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.
From time to time you get this error:
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
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)
** 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
Last edited: