Android Question getString

clooney48

Active Member
Licensed User
Longtime User
Hi Erel!

I am really desperate because simpliest things are so difficult.

So I have a field in a DB called "Pic" which contains the name of a pic (i.e. "1.jpg"). When I try to load it on the screen it works fine if the field "Pic" is not empty. But if it is empty I get an error although I check it before.

Code: Copy
If cursor1.getString("Pic") <> "" Then
        txtPic.Text = cursor1.getString("Pic")
        ImageView1.Bitmap = LoadBitmapSample(File.DirRootExternal, txtPic.Text, 10,110)
End If

regards
clooney48
 
Hi Klaus!

Didn´t work but thanks to your input this version worked:

Thanks
clooney48

Code: Copy
If  cursor1.getString("Pic") <> Null Then
        txtPic.Text = cursor1.getString("Pic")
        ImageView1.Bitmap = LoadBitmapSample(File.DirRootExternal & "/DCIM" & "/100Media", txtPic.Text, 10,110)
End If
 
Upvote 0
Are you sure that klaus code didn't work? It should work as the value is not null and not an empty string.

Yes, I tried both versions. As soon as I use this expression
Code: Copy
cursor1.getString("Pic") <> ""
I get an error and the code stops.
 
Upvote 0
Hi Klaus, hi Erel!

I just realized that this code also doesn´t work in any cases. So I analyzed it now.
I use a SQLite DB. There is shown a list with all entries. The problem occurs when I click an a list item:

Code: Copy
Sub LVDb_ItemClick (Position As Int, Value As Object)' click on the entry in the list
Dim idvalue As String
Dim countIt As Int
Dim vBild As String

idvalue = Value
ImageView1.Bitmap = Null
countIt = idvalue.IndexOf("|") 'find location of sperator
idvalue = idvalue.SubString2(0,countIt) 'find first part of label text
ID = idvalue
cursor1 = SQL1.ExecQuery("SELECT * FROM tblUsers where ID = '" & ID & "' ")
For i = 0 To cursor1.RowCount - 1
cursor1.Position = i
    txt1.text=cursor1.getString("Username")
    txt2.text=cursor1.getString("Password")
    txt3.text=cursor1.getString("Desc")
    vBild = cursor1.getString("Pic")
    If vBild  = "" OR vBild = Null Then 'CODE STOPS HERE
        vBild = "0.jpg"
    Else
        vBild = cursor1.getString("Pic")
    End If
        ImageView1.Bitmap = LoadBitmapSample(File.DirRootExternal & "/DCIM" & "/100Media", vBild, 10,110)
Next
End Sub

- When I click on the list to show an existing entry and there is a name of a pic in the field "Pic" it works with the expression: If vBild = "" OR vBild = Null Then
- When I click on the list to show an existing entry and there is NO name of a pic in the field "Pic" it works with the expression: If vBild = Null Then

The code stops always at the if-statement. Error message: java.lang.NullPointerExeption

Can you help me?
 
Upvote 0
Have you traced this code and check what the values indicate? This will give you a better idea of what the variables are being set to leading up to the point that it fails or at the very least generate with debug and get it to tell you the exact line in the Basic4Android code that is throwing the error, it might not be the one you currently suspect.
 
Upvote 0
Have you traced this code and check what the values indicate? This will give you a better idea of what the variables are being set to leading up to the point that it fails or at the very least generate with debug and get it to tell you the exact line in the Basic4Android code that is throwing the error, it might not be the one you currently suspect.


The code stops at the "If vBild = ""OR vBild = Null Then" line because one time the content of vBild is "" and one time the content is NULL. PIC is a field in the DB defined as text because it contains merely the name of the pic or nothing.

I think the problem is in the "Sub LVDb_ItemClick" procedure:

Code: Copy
Sub DBload
LVDb.Clear'need to clear the list
cursor1 = SQL1.ExecQuery("SELECT * FROM tblUsers")
For i = 0 To cursor1.RowCount - 1
cursor1.Position = i
LVDb.AddSingleLine(cursor1.GetString("ID")& "| " &cursor1.GetString("Username"))
LVDb.SingleLineLayout.ItemHeight = 99
LVDb.SingleLineLayout.Label.TextSize = 20
LVDb.SingleLineLayout.Label.TextColor = Colors.Black
LVDb.SingleLineLayout.Label.Color = Colors.White
Next
End Sub

Sub cmdAdd_Click
       
    If txt1.Text = "" Then
        Msgbox("You have to enter the name","Missed data field")
Else
   
    'Grab the last ID number which is the highest number
    cursor1 = SQL1.ExecQuery("SELECT ID FROM tblUsers")
    If cursor1.RowCount > 0 Then
        For i = 0 To cursor1.RowCount - 1   
    cursor1.Position = i
   
    Dim NewID As Int
    NewID = cursor1.GetInt("ID")
Next
End If
    NewID = NewID +1 ' add 1 to the ID number to make a new ID field
    SQL1.ExecNonQuery("INSERT INTO tblUsers VALUES('" & NewID & "','" & txt1.Text  & "','" & txt2.Text  & "','" & txt3.Text & "','" & txtPic.Text & "')")
    DBload
    txt1.Text = ""
    txt2.Text = ""
    txt3.Text = ""
    txtPic.Text = ""
    txtPOI.RequestFocus
End If
End Sub


Sub LVDb_ItemClick (Position As Int, Value As Object)' click on the entry in the list
Dim idvalue As String
Dim countIt As Int
Dim vBild As String

idvalue = Value
ImageView1.Bitmap = Null
countIt = idvalue.IndexOf("|") 'find location of sperator
idvalue = idvalue.SubString2(0,countIt) 'find first part of label text
ID = idvalue
cursor1 = SQL1.ExecQuery("SELECT * FROM tblUsers where ID = '" & ID & "' ")
For i = 0 To cursor1.RowCount - 1
cursor1.Position = i
    txt1.text=cursor1.getString("Username")
    txt2.text=cursor1.getString("Password")
    txt3.text=cursor1.getString("Desc")
    vBild = cursor1.getString("Pic")
    If vBild  = "" OR vBild = Null Then
        vBild = "0.jpg"
    Else
        vBild = cursor1.getString("Pic")
    End If
        ImageView1.Bitmap = LoadBitmapSample(File.DirRootExternal & "/DCIM" & "/100Media", vBild, 10,110)
Next
End Sub
 
Upvote 0
Isn't the OR bit the issue here.

If you check for it being NULL first, separately and then potentially set it to "" if it is, then execute the code, it should be fine.

I think the issue is when you encounter a NULL and the code attempts to resolve the first part of the If statement, which is NULL and the pointer exception is thrown as it tries to compare it to an empty string at this point.

It may well be better to define the field in SQLite as "" if Null. So then there aren't any NULLs to be handled.

Databases like NULLs but code that retrieves data from DBs almost hates it with a passion.
 
Upvote 0
I would follow eps' advice or replace this:
Code: Copy
If vBild = "" OR vBild = Null Then
 vBild = "0.jpg"
Else
 vBild = cursor1.getString("Pic")
EndIf
with:
Code: Copy
if cursor1.getString("Pic")="" or cursor1.getString("Pic")=Null then
  vBild = "0.jpg"
else
  vBild = cursor1.getString("Pic")
end if
Also, you want to get these below lines outside the loop to execute them only once:
Code: Copy
LVDb.SingleLineLayout.ItemHeight = 99
LVDb.SingleLineLayout.Label.TextSize = 20
LVDb.SingleLineLayout.Label.TextColor = Colors.Black
LVDb.SingleLineLayout.Label.Color = Colors.White
 
Upvote 0
Isn't the OR bit the issue here.

If you check for it being NULL first, separately and then potentially set it to "" if it is, then execute the code, it should be fine.

I think the issue is when you encounter a NULL and the code attempts to resolve the first part of the If statement, which is NULL and the pointer exception is thrown as it tries to compare it to an empty string at this point.

It may well be better to define the field in SQLite as "" if Null. So then there aren't any NULLs to be handled.

Databases like NULLs but code that retrieves data from DBs almost hates it with a passion.

Incredible, yes, this seems to work!
Code: Copy
If cursor1.getString("Pic") = Null OR cursor1.getString("Pic") = "" Then
also:
Code: Copy
If vBild = Null OR vBild  = "" Then

Thank you so much for your help, guys!
 
Upvote 0
That is great that you solved. I posted the same answer precisely the same time you were posting. But also, see the rest of my post for additional info.
 
Upvote 0
Incredible, yes, this seems to work!
Code: Copy
If cursor1.getString("Pic") = Null OR cursor1.getString("Pic") = "" Then

Thank you so much for your help, guys!
That is great that you solved. I posted the same answer precisely the same time you were posting. But also, see the rest of my post for additional info.


Yes, thanks! It is a good advice!
best regards
 
Upvote 0
Back
Top