Bug? String Returns Erroneous Length

MrKim

Well-Known Member
Licensed User
I have a type:
B4X:
    Type EmpType(Emp_Num As String, Emp_FirstName As String, Emp_LastName As String, Emp_AllowEdits As Boolean, Emp_AllowMulti As Boolean, Emp_Rate As Double, Emp_ShiftStart As String, Emp_ShiftEnd As String, EmpDate As String, Emp_Brake1Start As String, Emp_Brake1Length As Int, Emp_LunchStart As String, Emp_LunchLength As Int, Emp_Brake2Start As String, Emp_Brake2Length As Int, Emp_Wage As Double, PassWord As String)
Here is the running code:
upload_2019-5-10_23-14-13.png

Note the value and length of ActiveEmp.Password after ActiveEmp.Initialize.

After setting the value of Password to Null the length changes to 4!
upload_2019-5-10_23-18-48.png

I will point out that the PRIOR value of Password had a length of 4. But this should have cleared after initializing again.
 

MrKim

Well-Known Member
Licensed User
I think I see the problem. I posted a thread:
https://www.b4x.com/android/forum/threads/strange-behavior-of-null-value-returned-from-database.105706/
It would appear
B4X:
ActiveEmp.PassWord = Null
actually puts the word "null" into ActiveEmp.PassWord
The database actually sets the value TO Null:
B4X:
ActiveEmp.PassWord = Crsr.GetString("Emp_BadgeNum")
I see this now as TWO bugs.
1. Inconsistant behavior for Null. Null should set a Null value (as the return value of the database does), or fail if it can't because null is not valid.
2. The Debugger does not display any difference between the two. "null" is displayed as null and Null is displayed as null. This has caused me no end of grief trying to debug this. In the VBA debug window strings have quotes around them for clarity. That would help here as well. It would also help in distinguishing numbers from numeric strings.

Keep up the good work Erel. B4A is Awesome!
 

Daestrum

Well-Known Member
Licensed User
Null has always been 'odd' with strings.
That's why you will see
B4X:
If someString.Length = 0 ...
'or
If someString = "" ...
and not
B4X:
If someString = null ...
safest way to set an empty string is
B4X:
someString = ""
 

keirS

Well-Known Member
Licensed User
Not really that difficult to understand.

B4X:
someStiring = Null
Does not set a string to be null. It casts null to a string.
 

OliverA

Expert
Licensed User

Erel

Administrator
Staff member
Licensed User
This is not considered a bug. It is a programming mistake to set Null to a string.

The compiler automatically converts the unknown object type to a string and it gets converted to "null".
The default value of strings is an empty string, not Null. Overall you will not see many uses for Null in B4X.

There is a historic mistake in some libraries which do return Null for strings (SQL.ExecSingleResult for example). The behavior cannot be changed without breaking existing code.
 

MrKim

Well-Known Member
Licensed User
This is not considered a bug. It is a programming mistake to set Null to a string.

The compiler automatically converts the unknown object type to a string and it gets converted to "null".
The default value of strings is an empty string, not Null. Overall you will not see many uses for Null in B4X.

There is a historic mistake in some libraries which do return Null for strings (SQL.ExecSingleResult for example). The behavior cannot be changed without breaking existing code.
Please look at my other post:
https://www.b4x.com/android/forum/t...-of-null-value-returned-from-database.105706/
my issue at this point is that
B4X:
ActiveEmp.PassWord = Crsr.GetString("Emp_BadgeNum")
and
B4X:
ActiveEmp.PassWord = Null
Return different results.. It appears that ActiveEmp.PassWord actually IS getting converted to a Null, which you are saying shouldn't happen?
In VB I would simply do this:
B4X:
ActiveEmp.PassWord = Nz(Crsr.GetString("Emp_BadgeNum"),"")
But I can't figure out how to write an Nz function that will properly evaluate Crsr.GetString("Emp_BadgeNum")
Also, you are correct in VBA
B4X:
ActiveEmp.PassWord = Null
would throw an error, which is why i always Nz it.
Because it does NOT throw an error in B4A I was assuming that it was setting to null and trying to test for it. (It SAYS the value is null, but the length function is throwing an error.) Again, take a look at my other post.

Thanks for your patience - and brilliance. I don't know how you do it. I am in awe.:)
BTW Nulls ARE a pain in the ass, but they do have there use in the Data world. Without adding a separate field they are the only way to know that the user has not yet entered a value for that field. Sometime the value is NOTHING ("") but if it is null you want to prompt the user in case they forgot to fill it in. And this may happen later, AFTER the record has been saved but before proceeding to some other operation which depends on this record.
 

Erel

Administrator
Staff member
Licensed User
Return different results.. It appears that ActiveEmp.PassWord actually IS getting converted to a Null, which you are saying shouldn't happen?
Cursor.GetString returns a String type so the compiler sets it without any conversion. This is not the same as setting it with:
B4X:
Dim s As String = Null
Log(s.Length) '4 ("null")
The case with Cursor.GetString is an exception. This is one of the few cases where a string object can be set to Null.

As I wrote above, it is a mistake to set a string variable to Null in B4X. You should instead set it to an empty string. You can then check for empty value with:
B4X:
If s = Null OR s = "" Then 'Null from the database or "" from the default value
 'empty
End If
 
Top