walterf25 Expert Licensed User Longtime User Oct 23, 2025 #1 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
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 Oct 23, 2025 #2 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
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
JordiCP Expert Licensed User Longtime User Oct 23, 2025 #3 Also may be off here, but shouldn't it be 'and' instead of 'or'? Upvote 0
E emexes Expert Licensed User Longtime User Oct 24, 2025 #4 walterf25 said: before you guys yell at me Click to expand... Lol we'd never do that, and even if we did, we'd be yelling with you, not at you. walterf25 said: different variations such as the Null object as well as the String "null" Click to expand... 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: Code: ... Log("nextpageToken is " & GetType(nextpageToken)) Log((nextpageToken = Null)) Log((nextpageToken.As(Object) Is String)) If nextpageToken.Length <> 0 Or nextpageToken <> "null" Then ... Last edited: Oct 24, 2025 Upvote 0
walterf25 said: before you guys yell at me Click to expand... Lol we'd never do that, and even if we did, we'd be yelling with you, not at you. walterf25 said: different variations such as the Null object as well as the String "null" Click to expand... 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: Code: ... Log("nextpageToken is " & GetType(nextpageToken)) Log((nextpageToken = Null)) Log((nextpageToken.As(Object) Is String)) If nextpageToken.Length <> 0 Or nextpageToken <> "null" Then ...
aeric Expert Licensed User Longtime User Oct 24, 2025 #5 There are a few ways I would use. 1. Use IfNull(column_name, '') in database query 2. Switch your if-else with = instead of <> Code: If nextpageToken.Length = 0 Or nextpageToken = "null" Then 3. You may use EqualsIgnoreCase for checking "null" Upvote 0
There are a few ways I would use. 1. Use IfNull(column_name, '') in database query 2. Switch your if-else with = instead of <> Code: If nextpageToken.Length = 0 Or nextpageToken = "null" Then 3. You may use EqualsIgnoreCase for checking "null"
tchart Well-Known Member Licensed User Longtime User Oct 24, 2025 #6 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
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
D dyarker New Member Oct 25, 2025 #7 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
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)
JakeBullet70 Well-Known Member Licensed User Longtime User Oct 25, 2025 #8 Code I got some where. Code: Public Sub IsNullOrEmpty(s As String) As Boolean Return s = Null Or s.CompareTo(Null) = 0 Or s = "" End Sub Upvote 0
Code I got some where. Code: Public Sub IsNullOrEmpty(s As String) As Boolean Return s = Null Or s.CompareTo(Null) = 0 Or s = "" End Sub
tchart Well-Known Member Licensed User Longtime User Oct 25, 2025 #9 dyarker said: 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) Click to expand... 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
dyarker said: 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) Click to expand... 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.
TILogistic Expert Licensed User Longtime User Oct 25, 2025 #10 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 Click to expand... Upvote 0
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 Click to expand...
D dyarker New Member Oct 26, 2025 #11 tchart said: 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. Click to expand... 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
tchart said: 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. Click to expand... 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,
E emexes Expert Licensed User Longtime User Oct 26, 2025 #12 A more robust and efficient solution might be to eliminate the problem once at its source, when fetching strings from database eg: Code: 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: Code: 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: Oct 26, 2025 Upvote 0
A more robust and efficient solution might be to eliminate the problem once at its source, when fetching strings from database eg: Code: 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: Code: 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
JakeBullet70 Well-Known Member Licensed User Longtime User Oct 26, 2025 #13 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
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.
MrKim Well-Known Member Licensed User Longtime User Nov 17, 2025 #14 I have been using this for years: B4X: Sub Nz(Mval As Object, ValIfNull As Object) As Object Dim S As String Try S = Mval If S.Length=0 Or S = "null" Then Return ValIfNull Else Return Mval End If Catch Log(LastException.Message) ToastMessageShow("Nz" & CRLF & LastException.Message, True) Return "" End Try End Sub Upvote 0
I have been using this for years: B4X: Sub Nz(Mval As Object, ValIfNull As Object) As Object Dim S As String Try S = Mval If S.Length=0 Or S = "null" Then Return ValIfNull Else Return Mval End If Catch Log(LastException.Message) ToastMessageShow("Nz" & CRLF & LastException.Message, True) Return "" End Try End Sub