B4J Question Comparing or checking a String for "null" or Null

walterf25

Expert
Licensed User
Longtime User
Hi All, not sure if i'm having aneurysm, but i've always had issues with statements like the following

B4X:
    If nextpageToken.Length <> 0 Or nextpageToken <> "null" Then
        DownloadNextPage(nextpageToken, Common.Token)
    Else
        If buildList.IsInitialized And buildList.Size > 0 Then
            totalrecords = totalrecords + names.Size
            lblTotalRecords.Text = "Total Builds: " & totalrecords
            ''        Log("items: " & names)
            ''        Log("list size: " & names.Size)
            FillList(buildList)
        Else
            lblTotalRecords.Text = "0 Builds Found"
        End If
        ''B4XLoadingIndicator1.Hide
    End If
For some reason, regardless of the content of the nextpageToken string variable, that code still goes inside the first If statement, in this case the nextpageToken is "null", and I have tried different variations such as the Null object as well as the String "null" and as you guys can see I'm even checking the lenght of the string in case the string is empty, in the case where the string lenght is not 0 then the code does what's expected which is to go to the else statement, but when the string comes back as "null" it will obviously still go into the first If statement since the String lenght is greater than 0, not sure if anyone has seen or noticed this before, and before you guys yell at me, please let me know what my error is, or even better, what's the correct way to compare a string is?

Thanks,
Walter
 

Cableguy

Expert
Licensed User
Longtime User
I may be completely off in this but, I would try a few things...
1st, trim the string, to prevent a not zero length, but empty string
2nd, convert to lowercase, so that null and Null are the same

Somehow I was under the impression that a string that return "null" is a not initialized one, worth maybe add such condition too
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
before you guys yell at me

Lol we'd never do that, and even if we did, we'd be yelling with you, not at you. 

different variations such as the Null object as well as the String "null"

From memory, the problem is that if there is a string column in an SQL database that contains entries that have not been assigned, then those entries come back as Null objects rather than empty strings. I haven't found a way to reproduce Null-object Strings any other way.

What happens if you have a closer look at the variable eg:
B4X:
...
Log("nextpageToken is " & GetType(nextpageToken))
Log((nextpageToken = Null))
Log((nextpageToken.As(Object) Is String))
If nextpageToken.Length <> 0 Or nextpageToken <> "null" Then
...
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
There are a few ways I would use.

1. Use IfNull(column_name, '') in database query

2. Switch your if-else with = instead of <>
B4X:
If nextpageToken.Length = 0 Or nextpageToken = "null" Then

3. You may use EqualsIgnoreCase for checking "null"
 
Upvote 0

tchart

Well-Known Member
Licensed User
Longtime User
I use these two functions I wrote that have proven to be robust.

B4X:
Sub IsEmpty(Input As String) As Boolean
    If Input = Null Then Return True
    If Input.Length = 0 Then Return True
    If Input.ToLowerCase = "null" Then Return True
    Return False
End Sub

Sub IsNotEmpty(Input As String) As Boolean
    Return Not(IsEmpty(Input))
End Sub
 
Upvote 0

dyarker

New Member
Isn't "null" with quotes giving a 4 letter string?

I think you meant Chr(0) to give 1 character string with value of zero (a null string)
 
Upvote 0

tchart

Well-Known Member
Licensed User
Longtime User
Isn't "null" with quotes giving a 4 letter string?

I think you meant Chr(0) to give 1 character string with value of zero (a null string)
Not sure which post you are referring to but a string can be a real null or zero length string but sometimes eg with sql cursors it will return the string “null”.

Java primitive numbers can’t be null which is why in my function the input is a String and not object - just makes it a bit more reliable.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
and
??
B4X:
Public Sub IsNullOrEmpty(s As String) As Boolean
    Dim Invalids As List = Array ("", Null, "null", "undefined", "none", "n/a")
    For Each v As String In Invalids
        If s.Trim.ToLowerCase = v.ToLowerCase Then Return True
    Next
    Return False
End Sub

test:
B4X:
Public Sub test
    Dim a As String = Null
    Dim b As String = ""
    Dim c As String = "null"
    Dim d As String = "Hello"
   
    If IsNullOrEmpty(a)      Then Log("a is Null or empty")
    If IsNullOrEmpty(b)      Then Log("b is Null or empty")
    If IsNullOrEmpty(c)      Then Log("c is 'null' text")
    If Not(IsNullOrEmpty(d)) Then Log("d has a value: " & d)
End Sub

a is Null or empty
b is Null or empty
c is 'null' text
d has a value: Hello
 
Upvote 0

dyarker

New Member
post 1, source line 1-
B4X:
If nextpageToken.Length <> 0 Or nextpageToken <> "null" Then
ORing length 0 string with a null string made more sense than with the word null.

In post 6 source code line 2, you yourself used keyword Null instead of "null" for the comparison. Scanning other posts before mine, I did not see this pointed out.

In post 6 source line 4 you added a third IF for the different test for the word "null".

I was not criticizing any other posts. Had I specified post 1 in my previous it would have been clearer. Also unfamiliar with nextpageToken

Cheers,
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
A more robust and efficient solution might be to eliminate the problem once at its source, when fetching strings from database eg:

B4X:
Sub Denullify(PossiblyNullString As String) As String
    If PossiblyNullString = Null Then Return ""
    Return PossiblyNullString
End Sub

Dim rs As ResultSet = sql1.ExecQuery("select FirstName, LastName from People")
Do While rs.NextRow
    Dim FirstName As String = Denullify(rs.GetString("FirstName"))
    Dim LastName As String = Denullify(rs.GetString("LastName"))
    Log("""" & FirstName & """, """ & LastName & """")
Loop

rather than each time you refer to the problem variables throughout your processing code.

Can be done "manually" too eg:

B4X:
Dim rs As ResultSet = sql1.ExecQuery("select FirstName, LastName from People")
Do While rs.NextRow
    Dim FirstName As String = rs.GetString("FirstName") : If FirstName = Null Then FirstName = ""
    Dim LastName As String = rs.GetString("LastName") : If LastName = Null Then LastName = ""
    Log("""" & FirstName & """, """ & LastName & """")
Loop
 
Last edited:
Upvote 0

JakeBullet70

Well-Known Member
Licensed User
Longtime User
Don't forget JSON / XML from API's.
In working with Octoprint and getting JSON from it I would get "null" as a string to show nulls.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…