B4J Question buffer.Length vs Blob

T201016

Active Member
Licensed User
Longtime User
Hi,
I have a question about the code below, where I check the length of the buffer variable.
In this first case, I get an error (java.lang.NullPointerException),
while in the second example the source code is interpreted correctly.
So the question arises why I can use the "buffer.Length" instruction
only only when the field (Blob) is filled with a non-Null value?

Example 1:
            Dim buffer() As Byte = rs.GetBlob(ColumnName)
            ...
           
            Try
                If buffer.Length > 0 Then ----------- statement causing the error
                    ...
                End If
                Catch
                    Log(LastException.Message) 'java.lang.NullPointerException
            End Try

Example 2:
            Dim comparison As Boolean = (rs.GetBlob(ColumnName) = Null)
            If comparison Then 'Blob is Null!
 

T201016

Active Member
Licensed User
Longtime User
Thank you for your response, @Erel
following your obviously correct thought, and in the context of my presented line of code - an examination statement should be used, e.g. "If buffer.initialized = True/False Then ..." (which of course does not exist, it is replaced by Initialized(), of course). You understand? If there is such a thing as buffer.Length then there should be a true/false equivalent of buffer.Initalized, it would probably be better...(?) I'm considering it, purely theoretically.
I only care about the structure of the array instructions (i.e. instead of Initialize(Buffer) -> Buffer.Initialized)
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There are two recently new keywords in B4X: Initialized and NotInitiailized. Their purpose is to check the object state without relying on the object not being Null, as you can't call any method on a Null object.
The need for these keywords might be a bit confusing, as many types in B4X are "wrappers" and then they have the IsInitialized method that checks whether the wrapped object is Null or not.

Bottom line, you should prefer the two new keywords over the Object.IsInitialized method.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…