Android Question MQTT client random crashing

techknight

Well-Known Member
Licensed User
Longtime User
The crashing is random and super intermittent. So its hard to capture it in debug mode because its like the white lab coat syndrome, I cant get it to act up.

This time it crashed in release mode, and I had the logger connected.

Here is the crashlog:
B4X:
commengine_vvvvvvvvvvvvvvvvvvvvvvvv7 (java line: 218)
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:69)
    at md.vscbsoccerxl.commengine._vvvvvvvvvvvvvvvvvvvvvvvv7(commengine.java:218)
    at md.vscbsoccerxl.commengine._udp_packetarrived(commengine.java:846)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    at anywheresoftware.b4a.BA$2.run(BA.java:387)
    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:6823)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1557)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
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.BA.raiseEvent2(BA.java:250)
    at anywheresoftware.b4a.BA$2.run(BA.java:387)
    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:6823)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1557)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
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:69)
    at md.vscbsoccerxl.commengine._vvvvvvvvvvvvvvvvvvvvvvvv7(commengine.java:218)
    at md.vscbsoccerxl.commengine._udp_packetarrived(commengine.java:846)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    ... 8 more
--------- beginning of crash

This is the affected subroutine:
B4X:
public static String  _vvvvvvvvvvvvvvvvvvvvvvvv7() throws Exception{
anywheresoftware.b4j.objects.MqttAsyncClientWrapper.MqttConnectOptionsWrapper _mo = null;
//BA.debugLineNum = 126;BA.debugLine="Sub ConnectMQTT";
//BA.debugLineNum = 128;BA.debugLine="If mqtt.IsInitialized = False Then mqtt.Initializ";
if (_vvv1.IsInitialized()==anywheresoftware.b4a.keywords.Common.False) {
_vvv1.Initialize(processBA,"mqtt","tcp://"+_vv0+":1883",_vvv2);};
//BA.debugLineNum = 129;BA.debugLine="Dim mo As MqttConnectOptions";
_mo = new anywheresoftware.b4j.objects.MqttAsyncClientWrapper.MqttConnectOptionsWrapper();
//BA.debugLineNum = 130;BA.debugLine="mo.Initialize(\"\", \"\")";
_mo.Initialize("","");
//BA.debugLineNum = 131;BA.debugLine="If mqtt.Connected = True Then";
if (_vvv1.getConnected()==anywheresoftware.b4a.keywords.Common.True) {
//BA.debugLineNum = 132;BA.debugLine="mqtt.Close";
_vvv1.Close();
};
//BA.debugLineNum = 134;BA.debugLine="mqtt.Connect2(mo)";
_vvv1.Connect2((org.eclipse.paho.client.mqttv3.MqttConnectOptions)(_mo.getObject()));  <---This is the line the crash occurs on.
//BA.debugLineNum = 135;BA.debugLine="End Sub";
return "";
}

The BASIC code:
B4X:
Sub ConnectMQTT
'    ProgressDialogShow2("Connecting to Scoreboard. Please Wait...", False)
    If mqtt.IsInitialized = False Then mqtt.Initialize("mqtt", "tcp://" & IPAddress & ":1883", ClientID)
    Dim mo As MqttConnectOptions
    mo.Initialize("", "") 'UN/PW Not set yet on the server, but will be soon.
    If mqtt.Connected = True Then
        mqtt.Close
    End If
    mqtt.Connect2(mo)
End Sub

any ideas?
 

techknight

Well-Known Member
Licensed User
Longtime User
Note that the programming language is named B4X, not BASIC.

As I wrote, if you are closing the connection then you must initialize the client again.

Ok, thats strange behavior. I would have never assumed closing the connection would blow away the entire object which would require re-initialization again.

Anyways, at least now I know so I can fix it
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…