B4J Question [B4X] CreateMap and Null values

LucaMs

Expert
Licensed User
Longtime User
I found that CreateMap accepts Null values as well, like so:
B4X:
MyMap = CreateMap("Value":Null)
but If I had something like:
B4X:
MyMap = Create(Null)

Private Sub Create(Value As Map) As Map
    Return CreateMap("Value":Value)
End Sub
I would get an error: java.lang.RuntimeException: Object should first be initialized (Map) <--- Map Value, the parameter.

Obviously I got around this problem easily but I wonder if it is right for this to happen.
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
Your Sub is accepting a Map value not an object so that's why it's crashing. Null is not a Map.
That's not the problem, it's creating the Map that way.
You could have:
B4X:
    MyMap = Create2(Null)
    Log(MyMap)'ignore

Private Sub Create2(Value As Map) As Map
    Return Value
End Sub
without getting errors.

Change the type of Value to Object.
Obviously I got around this problem easily but I wonder if it is right for this to happen.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Change the type of Value to Object.
Obviously I got around this problem easily .
Setting the parameter type to Object is not a good choice; the developer needs to know what kind of data to pass.

More correctly I check that the past Map is initialized depending on whether it is or not...
B4X:
Private Sub Create(Value As Map) As Map
    If Value.IsInitialized Then
        Return CreateMap("Value":Value)
    Else
        Return CreateMap("Value":Null)
    End If
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…