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