New keyword - IsInitialized

Alessandro71

Well-Known Member
Licensed User
Longtime User
this seems to solve the null pointer exception that sometimes happens using Object.IsInitialized on a void object
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
It's a new internal function 😁
2 - WHYYYYYYY?
Its implementation is actually a bit more complicated than it might seem and even required an update the compiler itself.
Note that it will eventually be IsNotInitialized due to name conflicts. And the old ways will of course continue to work.
 

aeric

Expert
Licensed User
Longtime User
This is common for SQL object
B4X:
If SQL1 <> Null And SQL1.IsInitialized Then SQL1.Close
So
B4X:
If IsInitialized(SQL1) Then SQL1.Close
 

peacemaker

Expert
Licensed User
Longtime User
Syntax sugar, yuummmm😋, thanks !
 

peacemaker

Expert
Licensed User
Longtime User
But it is sometimes convenient to have a helper with the exact type reply.
I use in DBUtils.bas such functions:

B4X:
'return "", if no data
Sub GetStringFromSQL (SQL As SQL, req As String) As String
    Dim a As String
    Try
        a = SQL.ExecQuerySingleResult(req)
    Catch
        Return ""
    End Try

    If a = Null Then
        Return ""
    Else If a.ToLowerCase = "null" Then
        Return ""
    Else If a = "" Then
        Return ""
    Else
        Return a
    End If
End Sub

Sub GetBooleanFromSQL (SQL As SQL, req As String) As Boolean
    Dim a As String
    Try
        a = SQL.ExecQuerySingleResult(req)
    Catch
        Return False
    End Try

    If a = Null Then
        Return False
    Else If a.ToLowerCase = "null" Then
        Return False
    Else If a = "" Then
        Return False
    Else if a = "0" Or a.ToLowerCase = "false" Then
        Return False
    Else
        Return True
    End If
End Sub

'return -2147483648, if no valid number data
Sub GetIntFromSQL (SQL As SQL, table As String, req As String) As Int
    Dim Const NegativeRes As Int = -2147483648
    Dim q As Int = SQL.ExecQuerySingleResult("SELECT count(*) FROM " & table)
    If q = 0 Then
        Return NegativeRes
    End If
    Dim a As String
    Try
        'req may be wrong syntax, dangerous
        a = SQL.ExecQuerySingleResult(req)
    Catch
        Log("GetIntFromSQL(" & req & ").error = " & LastException)
        Return NegativeRes
    End Try

    If a = Null Then
        Return NegativeRes
    Else If a.ToLowerCase = "null" Then
        Return NegativeRes
    Else If IsNumber(a) = False Then
        Return NegativeRes
    Else
        Dim res As Int = a
        Return res
    End If
End Sub

'return -9223372036854775808, if no valid number data
Sub GetLongFromSQL (SQL As SQL, table As String, req As String) As Long
    Dim Const NegativeRes As Long = -9223372036854775808
    If TableExists(SQL, table) = False Then
        Log("GetIntFromSQL(" & table & ") does not exist")
        Return NegativeRes
    End If
    Dim q As Int = SQL.ExecQuerySingleResult("SELECT count(*) FROM " & table)
    If q = 0 Then
        Return NegativeRes
    End If
    Dim a As String
    Try
        'req may be wrong syntax, dangerous
        a = SQL.ExecQuerySingleResult(req)
    Catch
        Log("GetIntFromSQL(" & req & ").error = " & LastException)
        Return NegativeRes
    End Try

    If a = Null Then
        Return NegativeRes
    Else If a.ToLowerCase = "null" Then
        Return NegativeRes
    Else If IsNumber(a) = False Then
        Return NegativeRes
    Else
        Dim res As Long = a
        Return res
    End If
End Sub

Is it bad practice ?
 

tchart

Well-Known Member
Licensed User
Longtime User
B4X:
'old
If Lst = Null Or Lst.IsInitialized = False Then Return
'new
If IsInitialized(Lst) = False Then Return
Erel how will this affect custom Java libraries? Will it still call XYZ.IsInitialized under the hood? Or will we need to update our libraries to support this.
 

tchart

Well-Known Member
Licensed User
Longtime User
Yes thanks I already saw that but that doesn’t answer the question.

Yes the old ways are still supported but if we want to support IsInitialized(XYZ) is that a code change or is it handled transparently?
 

aeric

Expert
Licensed User
Longtime User
Yes thanks I already saw that but that doesn’t answer the question.

Yes the old ways are still supported but if we want to support IsInitialized(XYZ) is that a code change or is it handled transparently?
I expect it will be incorporated into the Core libraries on next version update for each platform (B4A, B4i, B4J).
 

b4x-de

Active Member
Licensed User
Longtime User
B4X:
'old
If Lst = Null Or Lst.IsInitialized = False Then Return
'new
If IsInitialized(Lst) = False Then Return
The most common case that requires our attention is an object that is NOT initialized (like your example shows). I would like to ask for a keyword IsNotInitalized() to make it even more easy. Thanks!
 

peacemaker

Expert
Licensed User
Longtime User
ask for a keyword IsNotInitalized()
Erel mentioned this new keyword required the update of the compilation process. So, i think it's already implemented and tested. And will not be inverted already.
So, just use like:
B4X:
If Not(IsInitialized(Lst)) Then Return
 
Top