B4J Question Questions regarding the judgment of the String type

hzq200409

Active Member
Licensed User
Hello everyone! During my programming practice, when I attempted to assign a column of null value from a MySQL database to a string variable, an error occurred when using this string variable again, and it was very difficult to pinpoint the row where the error occurred:
java.lang.Exception: java.lang.NullPointerException.
Without using the database statement "IFNULL(`field name`, '')" to specify a default value for it, how can I determine its length?
B4X:
Dim sqlstr As String = "SELECT * FROM `db1` WHERE `col1` = ? AND `col2` = ? ORDER BY `col4` DESC LIMIT 1;"
Dim Cursor As ResultSet = sql.ExecQuery2(sqlstr,Array As Object("xxx","xxxx"))
Dim success_time As String
Do While Cursor.NextRow
    success_time = Cursor.GetString("col1") 'col1 has a null value.
Loop
if success_time.length > 0 then 'The problem occurs in this line.
 

Daestrum

Expert
Licensed User
Longtime User
Null has no length method; you must check if its null before trying to get its length.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Without using the database statement "IFNULL(`field name`, '')" to specify a default value for it
Why you don't use IFNULL?
If the field type is Integer, use a default value. Eg. -1
IFNULL(success_time, -1)

Then check
B4X:
Dim success_time As Int = Cursor.GetInt("success_time") 
If success_time = -1 Then ...

if success_time = null

If it is a String type then the Null will converted to "Null"
If I don't want to check for the case sensitive, I will write as
B4X:
Dim success_time As String = Cursor.GetString("success_time")

If success_time.EqualsIgnorecase("null") Then ...
 
Upvote 0

hzq200409

Active Member
Licensed User
Why you don't use IFNULL?
If the field type is Integer, use a default value. Eg. -1
IFNULL(success_time, -1)

Then check
B4X:
Dim success_time As Int = Cursor.GetInt("success_time")
If success_time = -1 Then ...



If it is a String type then the Null will converted to "Null"
If I don't want to check for the case sensitive, I will write as
B4X:
Dim success_time As String = Cursor.GetString("success_time")

If success_time.EqualsIgnorecase("null") Then ...
Thank you for your attention.This is merely a discussion about the null pointer issue caused by an empty string. The intention is to simply avoid it from the perspective of B4X. Because this kind of problem doesn't only occur during database queries. Because I'm not familiar with B4X, I don't know if there are any other languages that have a similar method like "is_null" for checking. Suddenly, I realized that using '=' to determine NULL is actually so simple.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
If you assign the null value to a String then it will be converted to String.
If you expect the value you want to test is a null then perhaps you can assign it to an object and use Initialized or NotInitialized to test it.
B4X:
Dim obj1 As Object = Null
Log( Initialized(obj1) )
Log( NotInitialized(obj1) )

These internal keywords are introduced in B4J v10.2 - March 17, 2025
 
Upvote 0
Top