Android Question Value null in float or Integer

rossati

Active Member
Licensed User
Longtime User
Hello

why I can assign null value to a float (or Integer) variable and I can't test it?
B4X:
    Dim integer As Int = Null ' this works
    If integer = Null Then ... ' this crashes
Regards
Giovanni Rossati
 

MarkusR

Well-Known Member
Licensed User
Longtime User
"int" is not an object/class , so i guess it cast into value 0.
me would also expect the same behavior.
integer did not have null.
the compiler should catch this.

Logger verbunden mit: motorola XT1032
--------- beginning of main
Copying updated assets files (3)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 273 (Main)
java.lang.NumberFormatException: Invalid double: "null"
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.initialParse(StringToReal.java:164)
at java.lang.StringToReal.parseDouble(StringToReal.java:282)
at java.lang.Double.parseDouble(Double.java:301)
at anywheresoftware.b4a.BA.ObjectToNumber(BA.java:646)
at com.rauch.test.main._null_click(main.java:633)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at anywheresoftware.b4a.BA$1.run(BA.java:325)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
 
Last edited:
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi rossati,

I did some experimenting and discovered you can't assign Null to any numeric variables but you can do it to strings. For numeric variables you can only assign a number to them such as 0 or some other number. The "If" statement never got a chance to execute. I commented out the coding that will make your app crash.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

    Dim strMyString As String = Null

    If strMyString = Null Then
        ' This works.
    End If

'  Dim lngMyLong as Long = Null

'    Dim integer As Int = Null ' this works
    
'    If integer = Null Then
'          ' this crashes
'    End If
End Sub
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
You can assign null to variables that work by reference, all numeric variables or basic variables work by value and not by reference

Objects, classes, or non-basic variables work as references and can be assigned with null
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
yes, this basic types are exceptional case in OOP ^^
 
Upvote 0
Top