Android Question Problem with an integer compare (Crash)

fbritop

Active Member
Licensed User
Longtime User
Hi
This has really no explanaition to me.
Why if at line 331
B4X:
Log(bookingTypeMap.Get("showOnBooking").As(Int)=1)

It crashes on the next line (If checks at 331)
B4X:
If bookingTypeMap.Get("showOnBooking").As(Int)=1 Then
I have checked that both are the same type, that both values are not null but i cannot see why this would happen. I also tried to change the type to Long with no success either. Why the logs output false, but the IF crashes

B4X:
    Dim idBookingType As Int=bookingType.Tag.As(Int)
    For i=0 To Main.mapData.Get("bookingTypes").As(List).Size-1
        Dim bookingTypeMap As Map=Main.mapData.Get("bookingTypes").As(List).Get(i).As(Map)
        Log(bookingTypeMap)
        Log(GetType(bookingTypeMap.Get("idBookingType")))
        Log(bookingTypeMap.Get("showOnBooking").As(Int)=1)
        If bookingTypeMap.Get("showOnBooking").As(Int)=1 Then '<---LINE 332
            Try
                If idBookingType=bookingTypeMap.Get("idBookingType").As(Int) Then
                    sb.Append(". Reserva tipo " & bookingTypeMap.Get("bookingType"))
                    If listData.GetPanel(instructorIndex).Height>1 Then
                        sb.Append(", instructor " & instructor.Title)
                    End If
                End If
            Catch
                Log(LastException)
            End Try
        End If
    Next

B4X:
(MyMap) {idBookingType=1134, bookingType=Normal, bookingTypeCode=NORMAL, showOnBooking=1, showOnFlightLog=1, hasCharge=1, bookingTypeClass=blue, bookingTypeColor=0,64,255}
java.lang.Integer
true
(MyMap) {idBookingType=1135, bookingType=Instrucción, bookingTypeCode=INSTRUCTION, showOnBooking=1, showOnFlightLog=1, hasCharge=1, bookingTypeClass=green, bookingTypeColor=0,140,35}
java.lang.Integer
true
(MyMap) {idBookingType=1133, bookingType=Chequeo, bookingTypeCode=CHECK, showOnBooking=1, showOnFlightLog=1, hasCharge=1, bookingTypeClass=orange, bookingTypeColor=255,127,0}
java.lang.Integer
true
(MyMap) {idBookingType=2799, bookingType=Examen DGAC, bookingTypeCode=DGAC, showOnBooking=1, showOnFlightLog=1, hasCharge=1, bookingTypeClass=brown, bookingTypeColor=213,144,120}
java.lang.Integer
true
(MyMap) {idBookingType=4720, bookingType=PAX Club, bookingTypeCode=PAXC, showOnBooking=0, showOnFlightLog=1, hasCharge=0, bookingTypeClass=teal, bookingTypeColor=51,153,153}
java.lang.Integer
false
(MyMap) {idBookingType=133037, bookingType=Demo, bookingTypeCode=DEMO, showOnBooking=0, showOnFlightLog=1, hasCharge=0, bookingTypeClass=purple, bookingTypeColor=204,0,204}
java.lang.Integer
false
(MyMap) {idBookingType=145294, bookingType=Mantenimiento, bookingTypeCode=MANTTO, showOnBooking=0, showOnFlightLog=1, hasCharge=0, bookingTypeClass=dark, bookingTypeColor=0,0,0}
java.lang.Integer
false
(MyMap) {idBookingType=1895908, bookingType=No Disponible, bookingTypeCode=NA, showOnBooking=0, showOnFlightLog=1, hasCharge=0, bookingTypeClass=cyan, bookingTypeColor=0,0,0,0}
java.lang.Integer
false
Error occurred on line: 332 (userBookingsAdd)
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference
    at anywheresoftware.b4a.BA.switchObjectToInt(BA.java:792)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:1114)
    at anywheresoftware.b4a.keywords.Common.CallSubNew(Common.java:1061)
    at cl.planeadores.android.clsbutton._btn2_click(clsbutton.java:372)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 

Daestrum

Expert
Licensed User
Longtime User
try putting the constant on the left
B4X:
If 1 = bookingTypeMap.Get("showOnBooking").As(Int) Then
...
'or
If (bookingTypeMap.Get("showOnBooking").As(Int) = 1) then 
....
but it's probably because the value returned is null so you should check for it or use the default value get and set it to something obvious like -999.


If I remember correctly, if you ask for a key that doesn't exist you get null returned.
 
Last edited:
Upvote 0

fbritop

Active Member
Licensed User
Longtime User
try putting the constant on the left
B4X:
If 1 = bookingTypeMap.Get("showOnBooking").As(Int) Then
...
'or
If (bookingTypeMap.Get("showOnBooking").As(Int) = 1) then
....
but it's probably because the value returned is null so you should check for it or use the default value get and set it to something obvious like -999.


If I remember correctly, if you ask for a key that doesn't exist you get null returned.
Thanks
But I tried before the constant on the right

if I log this, it returns the value that is correct from the map: 0

B4X:
Log(bookingTypeMap.Get("showOnBooking"))
Log(bookingTypeMap.Get("showOnBooking").As(Int))

I also tried:
B4X:
Log(mapBookingType.Get("showOnBooking").As(Int)=Null)
Which logs False
 
Upvote 0

fbritop

Active Member
Licensed User
Longtime User
This is getting far behind my knowledge, not sure if is a BUG
The error gets generated just before the IF statement closes with an END IF


B4X:
    Try
        If listData.GetPanel(instructorIndex).Height>1 And instructor.Tag Is Map Then
            Dim instructorMap As Map=instructor.Tag
            Log(instructorMap)
            sb.Append(", instructor ")
            Log(instructorMap.Get("name"))
            sb.Append(instructorMap.Get("name"))
            sb.Append(" ")
            Log(instructorMap.Get("lastName"))
            sb.Append(instructorMap.Get("lastName"))
            sb.Append(instructorMap.Get("lastName"))
            sb.Append(instructorMap.Get("lastName"))
            sb.Append(instructorMap.Get("lastName"))
            Log(instructorMap.Get("lastName")) '<---- ERROR         
        End If
    Catch
        Log(LastException)
    End Try

If I remove the line that throws the error:
B4X:
Log(instructorMap.Get("lastName"))

The error know move one line before and the last
B4X:
Log(instructorMap.Get("lastName"))
now throws the error.

In all cases, it outputs this error, regardless of it is ant Int, string or whatever
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference

The error also does not fire within the CATCH

If I add this line before the END IF
B4X:
Log("TEST") '<---- ERROR MOVES HERE
Now the error moves here!

Other actions taken, PC restarted, IDE restarted, proyect clean, etc

Clueless at this point!
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Try it without the .as(int)

Java 'should' autobox it for the comparison.
 
Upvote 0

fbritop

Active Member
Licensed User
Longtime User
Try it without the .as(int)

Java 'should' autobox it for the comparison.
Thanks
But it does not matter, as in the last code in my last post, the error always moves just before the END IF
Even with strings attachingto the StringBuilder or even putting Log("TEST")
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
see
Can I get help using GetDeault?
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
maybe stupid idea but check it

B4X:
Dim strValue as String,intValue as Int

If Map.ContainsKey("showOnBooking")=False Then
    Log("Key showOnBooking is missing")
    Return
End If

strValue=bookingTypeMap.Get("showOnBooking")
Log("strValue=" & strValue)

If IsNumber(strValue)=False Then
    Log("Value " & strBalue & " is not numeric")
    Return
End If

intValue=strValue
 
Upvote 0

fbritop

Active Member
Licensed User
Longtime User
I should not mark it as "Solved", as I don´t know inside the black box what was happening. I just reinstalled B4A and the problem went away....:oops:

Cannot figure it out but thank anyway to all of you
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
There is a 'hacky' way to force a comparable value
B4X:
map.put ("a" , "1")

if (map.get("a") = 1) then ....   ' will fail the test  - String vs Integer
...
if (map.get("a")&"" = 1 ) then     ' will work and wont crash even if a is not in the map "a" in the map can be "1" or 1
 
Upvote 0
Top