B4J Question NullPointerException MQTT error in DEBUG mode [solved]

jmon

Well-Known Member
Licensed User
Hello

Since yesterday I started having some errors that I've never seen, and can't find where the problem is. The error seems to be cause by MQTT, when I receive a message from clients.

This is the error:
B4X:
...
# Connected: true
Error occurred on line: 65
java.lang.NullPointerException
    at anywheresoftware.b4a.shell.Shell.setStateBeforeUserSub(Shell.java:387)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:288)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
    at sun.reflect.GeneratedMethodAccessor63.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:79)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:92)
    at anywheresoftware.b4a.BA$3.run(BA.java:219)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
Error occurred on line: 65
java.lang.NullPointerException
    at anywheresoftware.b4a.shell.Shell.runGoodChain(Shell.java:358)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:169)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
    at sun.reflect.GeneratedMethodAccessor63.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:79)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:92)
    at anywheresoftware.b4a.BA$3.run(BA.java:219)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)

This is my line 65, it's actually the first line of a sub:
B4X:
(65) Sub Client_MessageArrived (Topic As String, Payload() As Byte)
    ....

My app is both a Broker (jMQTTBroker) and a Client. I think this error is raised when the broker-client sends a message to everyone, including itself. When other clients send a message to my broker I don't get an error.

What is strange is that this error happens in debug mode several times in a row, but then when I switch to release, the error is gone.

Also strange is that I never seen any error like this until yesterday, but I didn't change that part of the code yesterday...

Any idea?

Thank you for your help.

[EDIT]
it happens with any client, not only when the broker sends itself a message

[EDIT]
seems to happen only in debug mode, works in release
 
Last edited:

jmon

Well-Known Member
Licensed User
after testing more, it seems to be caused by this sub (in the master broker-client app):
B4X:
Sub Client_Connected (Success As Boolean)
    Log($"# Connected: ${Success}"$)
    If Success Then
        'Success, then connect to the topic:
        Client.Subscribe("master/#", QOS)
        Send("clients/whosConnected", "")
    Else
        TimerConnect.Enabled = True
    End If
End Sub

This is when my broker-client is connected to the broker (itself). After being connected, the broker-client sends a message to the "clients/" to ask who is connected. The clients reply with a simple message so that this broker-client can build a list of clients.

the error happens when a client replies to that Send("clients/whosConnected", "") message and the sub "Client_MessageArrived" is raised in the broker-client app.

this is how the clients reply:
B4X:
Sub Client_MessageArrived (Topic As String, Payload() As Byte)
    Dim msg As Message = serializator.ConvertBytesToObject(Payload)
        Select Topic
            Case "clients/whosConnected"
                Send("master/connected", "")
'....
End Sub

Sub Send(Topic As String, Payload As Object)
    If Client.Connected Then
        Client.Publish2(Topic, serializator.ConvertObjectToBytes(CreateMessage(Payload)), 0, False)
    Else
        Log($"Error sending message: NOT CONNECTED"$)
    End If
End Sub

Sub CreateMessage(Payload As Object) As Message
    Dim msg As Message
    msg.Initialize
    msg.Payload = Payload
    msg.FromClient = Main.CLIENT_ME
    msg.Id = Misc.GenerateUniqueId
    msg.TimestampSent = DateTime.Now
    Return msg
End Sub
 
Last edited:
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Hi!!

I had a not so related error, may be it is completly different, for you to judge.

Recently i released an app that communicates via HTTP, but for whatever reason, after some time it just fails with the following error showing on CMD

B4X:
main._appstart (java line: 65)

java.lang.RuntimeException: java.io.FileNotFoundException: C:\Users\Raul\AppData

\Local\Temp\16 (Acceso denegado)

  at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)

  at anywheresoftware.b4a.BA$3.run(BA.java:219)

  at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(Simple

MessageLoop.java:30)

The problem is that StartMessageLoop is called once, but it does not happen when it is called but after some time (a timer is running in background and i am sure this timer is never calling StartMessageLoop)

But the most strange is that I do not have any call to a file.

well...EVEN STRANGER is that the error happens when i make an HTTP call, but the log (that is just after the sub) is never reached.

B4X:
Sub JobDone (Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)


To partially solve the issue i changed:

B4X:
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    LogError(StackTrace)
    Return False ' was true
End Sub
 
Upvote 0

jmon

Well-Known Member
Licensed User
Is it easy to reproduce with your project? Can you send me your project?
Thank you for your help. I'll see if that can be reproduced with a smaller project and send it to you.
 
Upvote 0

jmon

Well-Known Member
Licensed User
Upvote 0
Top