T201016 Active Member Licensed User Longtime User Apr 12, 2026 #1 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? Code: Copy 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 Code: Copy Dim comparison As Boolean = (rs.GetBlob(ColumnName) = Null) If comparison Then 'Blob is Null!
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? Code: Copy 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 Code: Copy Dim comparison As Boolean = (rs.GetBlob(ColumnName) = Null) If comparison Then 'Blob is Null!
Erel B4X founder Staff member Licensed User Longtime User Apr 12, 2026 #2 A Null field returns Null, not a zero length array. 1. Better to remove the Try / Catch block. 2. Code: Copy If Initialized(Buffer) And Buffer.Length > 0 Then Upvote 0
A Null field returns Null, not a zero length array. 1. Better to remove the Try / Catch block. 2. Code: Copy If Initialized(Buffer) And Buffer.Length > 0 Then
T201016 Active Member Licensed User Longtime User Apr 12, 2026 #3 Erel said: A Null field returns Null, not a zero length array. 1. Better to remove the Try / Catch block. 2. Code: Copy If Initialized(Buffer) And Buffer.Length > 0 Then Click to expand... 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: Apr 12, 2026 Upvote 0
Erel said: A Null field returns Null, not a zero length array. 1. Better to remove the Try / Catch block. 2. Code: Copy If Initialized(Buffer) And Buffer.Length > 0 Then Click to expand... 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)
Erel B4X founder Staff member Licensed User Longtime User Apr 13, 2026 #4 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
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.
T201016 Active Member Licensed User Longtime User Apr 13, 2026 #5 Erel said: Bottom line, you should prefer the two new keywords over the Object.IsInitialized method. Click to expand... Thanks for the reply, I'll take a closer look at them before I start preferring them. Upvote 0
Erel said: Bottom line, you should prefer the two new keywords over the Object.IsInitialized method. Click to expand... Thanks for the reply, I'll take a closer look at them before I start preferring them.