B4J Question MQTT Map Initialize Error

Orejana

Member
Hi everyone,
I'm using MQTT to get messages from The Things Network from two sensors. I then put them into a map which works perfectly fine - usually.
For some reason, two days ago my program crashed and sent me an error message saying my map wasn't initialized. I basically ignored it, but now it came up again. Has anyone got an idea what I can do to avoid this?

EDIT: The program both times had run for several minutes and received several messages from the network before crashing

B4X:
Sub mqtt_MessageArrived (mqtttopic As String, Payload() As Byte)
  
   Log(BytesToString(Payload, 0, Payload.Length, "utf8"))
   JSON.Initialize(BytesToString(Payload, 0, Payload.Length, "utf8"))
   Map1 = JSON.NextObject
  
   m.Initialize
  
   m = Map1.Get("payload_fields")
   Log(m)
   moisture = m.Get("moisture")
   temp = m.Get("temperature")
  
   Log(Round2(moisture,1))
   Log(Round2(temp,1))
End Sub

Error message:

(Map) Not initialized
Error in line: 279 (Main)
java.lang.RuntimeException: Object should first be initialized (Map).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:32)
at anywheresoftware.b4a.objects.collections.Map.Get(Map.java:67)
at b4j.example.main._mqtt_messagearrived(main.java:1230)
at sun.reflect.GeneratedMethodAccessor5.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:625)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:168)
at sun.reflect.GeneratedMethodAccessor3.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:90)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:94)
at anywheresoftware.b4a.BA$3.run(BA.java:246)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Where is the output of this line:
B4X:
 Log(BytesToString(Payload, 0, Payload.Length, "utf8"))
Change it to:
B4X:
Log("Input: " & BytesToString(Payload, 0, Payload.Length, "utf8"))
Otherwise you will miss it if you receive an empty string.

B4X:
  m.Initialize
  
   m = Map1.Get("payload_fields")
You should never initialize an object and then assign a new object to the same variable.
 
Upvote 0

Orejana

Member
Ok, thanks.
Usually:
Input: {"app_id":"arduino_rfm95_test","dev_id":"uwa_node_1","hardware_serial":"0054333F0D8277AF","port":1,"counter":77636,"payload_raw":"AQoAlw==","payload_fields":{"moisture":15.1,"port":1,"temperature":26.6},"metadata":{"time":"2018-08-16T15:41:47.115802145Z","frequency":868.1,"modulation":"LORA","data_rate":"SF9BW125","airtime":164864000,"coding_rate":"4/5","gateways":[{"gtw_id":"eui-b827ebfffe0e6a1e","timestamp":3190849076,"time":"2018-08-16T15:41:47.072087Z","channel":0,"rssi":-90,"snr":11.8,"rf_chain":1,"latitude":49.44966,"longitude":8.6048,"altitude":100}],"latitude":49.449665,"longitude":8.605012,"altitude":100,"location_source":"registry"}}
(MyMap) {port=1, temperature=24.4, moisture=14.1}

The message where the error occurs:
Input: {"app_id":"arduino_rfm95_test","dev_id":"uwa_node_1","hardware_serial":"0054333F0D8277AF","port":1,"counter":77637,"payload_raw":"AQoAlw==","metadata":{"time":"2018-08-16T15:42:45.672333446Z","frequency":868.3,"modulation":"LORA","data_rate":"SF9BW125","airtime":164864000,"coding_rate":"4/5","gateways":[{"gtw_id":"eui-b827ebfffe0e6a1e","timestamp":3249395780,"time":"2018-08-16T15:42:45.620833Z","channel":1,"rssi":-91,"snr":11.8,"rf_chain":1,"latitude":49.44966,"longitude":8.6048,"altitude":100}],"latitude":49.449665,"longitude":8.605012,"altitude":100,"location_source":"registry"}}
(Map) Not initialized


Any ideas why the payload fields might be missing?
 
Last edited:
Upvote 0
Top