B4J Question Bug in B4J or something I do not understand?

hatzisn

Well-Known Member
Licensed User
Longtime User
Good evening everyone,

I think I have found a bug in B4J or there is something I do not understand completely.
I create a WebApp which accepts a posted JSON and gets a "ClientID" key from the derived from JSON map. Consider this code:

B4X:
Dim m as Map
Try
    m=sJSON.As(JSON).ToMap
    If m.get("ClientID") = -1 then
        'Add the client
    Else
        'Update the client
    End if
Catch
    Log(LastException)
End Try

If I place a breakpoint in the "Try" line and I have sent a ClientID = -1 then it executes correctly ADD function everytime.
If now I try without a breackpoint in the "Try" line and I have sent a ClientID = -1 then it executes correctly ADD function the first time and from the next and on it executes wrongly the UPDATE client function (????? - Quantum effects in B4J ? :) ). I send the requests with Postman.

Any suggestions?
 

Star-Dust

Expert
Licensed User
Longtime User
Due to the casting, the comparison may not be positive
B4X:
Dim m as Map
Try
    m=sJSON.As(JSON).ToMap
    If m.get("ClientID").As(Int) = -1 then
        'Add the client
    Else
        'Update the client
    End if
Catch
    Log(LastException)
End Try

or
B4X:
Dim m as Map
Try
    m=sJSON.As(JSON).ToMap
    If -1 = m.get("ClientID") then
        'Add the client
    Else
        'Update the client
    End if
Catch
    Log(LastException)
End Try
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Don't ignore the warning:

1669218262800.png

Change your code to If -1 = m.get("...") Then

What happens is that in some cases -1 can be treated as integer and in some cases it can be treated as double. As the warning says, the comparison will fail if the types do not match exactly. The solution is to switch the operands. This way the compiler will know the expected type and will cast it properly.

Edit: I was ninja'd :)
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Edit: I was ninja'd :)

It's a great thing that I can get answers for all the things that bother me from a ninja. :)
 
Upvote 0
Top