Android Question How do I validate for Null value when a SQLite BLOB is empty/null?

BvdB

Member
Licensed User
Longtime User
Hi all clever people,

I hope some-one can assist me with my 'challenges' pertaining to images in SQLite BLOBs
I have a question, but let me first briefly explain what I try to achieve...

I want to load an image from a BLOB field in a SQLite database table and display it as an image on screen.

All the samples and discussions that I read, load the bitmaps from storage and not from a database, which is not what I want to do here.
I managed to get it working but I have records in the table that may not contain an image. Therefore I need to validate if the BLOB field is Null, or I need to ensure I store a very small dummy image if an valid image is not saved and/or carry another Boolean field which indicates whether a image is present or not.
I don't want to do this unless there is no other way of validating for an empty (Null) BLOB field in the Database.

The following code works 100% if an image is present in the BLOB field:
B4X:
Dim readCursor as Cursor 
Dim imgBuffer as Byte 
Dim imgInputStream  as InputStream 
Dim imgBitmap as Bitmap 
Dim imgOnScreen as ImageView 

...the  SQL statements to populate the readCursor.... 

imgBuffer = readCursor.GetBlob ("ImageCol") 
'before I proceed from here, I want to validate if the imageBuffer is Null, 
'because it can be  empty (containing no image). 

If imgBuffer = Null then ' this is what I need to get solved 
     'skip and don't load anything else
Else
     imgInputStream.InitiaizeFromBytesArray (imgBuffer, 0, imgbuffer.Length) 
     imgBitmap.Initialize (imgInputStream) 
     imgInputStream.Close 
     imgOnScreen.Bitmap = imgBitmap 
End If
How do I validate the imgBuffer for Null value?
I tried various options for example:
Test for Null

B4X:
....
If imgBuffer.Length < 1 then<br> ...or... If imgBuffer = Null then

but at runtime I get the error: java.lang.NullPointerException...

So I tried to use a sub to validate:

B4X:
... If isNullBuffer (imgBuffer) = False then
... End If

Sub isNullBuffer (bufferValue as Object) as Boolean
Try 
If bufferValue <> Null then 
      Return False 
End If

Catch 
     Return True 
End Try 
End Sub

... but during execution, after executing the If statement, the caret jumps straight to the End Try statement.
reached the End Sub, return a True value by default and then throw an exception on imgInputStream.InitiaizeFromBytesArray.....

How do I validate for a Null value in a BLOB or Byte?
 

KPmaster

Member
Licensed User
Longtime User
Did you try something like this?

B4X:
    ImgBuffer = Null
    If readCursor.GetBlob("ImageCol") <> Null Then
        ImgBuffer = readCursor.GetBlob("ImageCol")
    End If
  
    If ImgBuffer = Null Then
        ' your code here
    Else
        ' your code here
    End If
 
Upvote 0

BvdB

Member
Licensed User
Longtime User
Did you try something like this?

B4X:
    ImgBuffer = Null
    If readCursor.GetBlob("ImageCol") <> Null Then
        ImgBuffer = readCursor.GetBlob("ImageCol")
    End If
 
    If ImgBuffer = Null Then
        ' your code here
    Else
        ' your code here
    End If

:confused: I made the change as you suggested KPmaster and it WOOOORRRRKS!!! :D :D

Thank you for the help, it is much appreciated!
 
Upvote 0

Fifi Donkor

Member
Licensed User
Longtime User
Although this is an old thread, I would like to make my contribution for the sake of others looking for this answer. I have a piece of code that i wrote under inspiration from VB.Net's Nz() function. I keep it in a code module called fnx.bas so you can put it in your own module and rename the prefix.

B4X:
    Sub Nz(ObjToCheck As Object,ReturnIfNull As Object ) As Object
        If ObjToCheck <> Null    Then
            Return ObjToCheck
        Else
            Return ReturnIfNull
        End If
    End Sub

It appears to work with anything i throw at it. Just pass the object you're checking and what to return if that object is null. Like...

B4X:
Value = Nz( IntNumber, 0 )

Above will return 0

B4X:
Value = Nz( StringText, "" )

Another example. I have tried to make this as newbie friendly as possible.
 
Upvote 0

mr23

Active Member
Licensed User
Longtime User
Upvote 0
Top