1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

Bug? String Returns Erroneous Length

Discussion in 'Bugs & wishlist' started by MrKim, May 11, 2019.

  1. MrKim

    MrKim Active Member Licensed User

    I have a type:
    Code:
    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.
     
  2. MrKim

    MrKim Active Member Licensed User

    I think I see the problem. I posted a thread:
    https://www.b4x.com/android/forum/t...-of-null-value-returned-from-database.105706/
    It would appear
    Code:
    ActiveEmp.PassWord = Null
    actually puts the word "null" into ActiveEmp.PassWord
    The database actually sets the value TO Null:
    Code:
    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!
     
  3. Daestrum

    Daestrum Well-Known Member Licensed User

    Null has always been 'odd' with strings.
    That's why you will see
    Code:
    If someString.Length = 0 ...
    'or
    If someString = "" ...
    and not
    Code:
    If someString = null ...
    safest way to set an empty string is
    Code:
    someString = ""
     
  4. keirS

    keirS Well-Known Member Licensed User

    Not really that difficult to understand.

    Code:
    someStiring = Null
    Does not set a string to be null. It casts null to a string.
     
    OliverA likes this.
  5. OliverA

    OliverA Expert Licensed User

    Yes, and when GetString returns a Null it returns a pointer to a Null object that is assigned to (in this case) ActiveEmp.Password. This object then has no methods which is why the Debugger shows that the (Length) method does not exist (see @MrKim's link). Someone's opinion about Null: https://www.lucidchart.com/techblog/2015/08/31/the-worst-mistake-of-computer-science/
     
    José J. Aguilar likes this.
  6. Erel

    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.
     
  7. MrKim

    MrKim Active Member Licensed User

    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
    Code:
    ActiveEmp.PassWord = Crsr.GetString("Emp_BadgeNum")
    and
    Code:
    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:
    Code:
    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
    Code:
    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.
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    Cursor.GetString returns a String type so the compiler sets it without any conversion. This is not the same as setting it with:
    Code:
    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:
    Code:
    If s = Null OR s = "" Then 'Null from the database or "" from the default value
     'empty
    End If
     
    MrKim likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice