Android Question [Solved] NullPointerException in DBUtils.ExecuteToList

asales

Expert
Licensed User
Longtime User
I try to use this code below to fill a list using DBUtils.ExecuteToList and if the list is null, I fill the list using add the values manually, but I get this error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void anywheresoftware.b4a.objects.collections.List.AddAll(anywheresoftware.b4a.objects.collections.List)' on a null object reference

How I can do this (if list is null, fill it using other values)?
B4X:
Dim lst As List
lst = DBUtils.ExecuteToList(Starter.SQL1, "Select name FROM contacts ORDER BY name", "name", 0, 0)
If (lst = Null) Then
    ToastMessageShow("There is no names in the list."&CRLF&"Using default values.", False)
    'lst.Initialize
    lst.AddAll(Array As String("Name 01","Name 02","Name 03"))
End If

If I initialized the list (lst.Initialize) in If...Then I get this error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void anywheresoftware.b4a.objects.collections.List.Initialize()' on a null object reference

Thanks in advance for any tip.
 

mc73

Well-Known Member
Licensed User
Longtime User
Which version of dbUtils are you using? I've just downloaded the class version 1.10 and didn't see the executeToList sub. Strange...
 
Upvote 0

asales

Expert
Licensed User
Longtime User
I see now. This is a modified version (I don't know where I found it).

The module is in attached.

This is the code to ExecuteToList:
B4X:
'Modified: 07.05.2013 Jhai Salvador
'Executes the query and fills the list with the values in the first column
'FieldToShow is the column you want to show on the list
'put 0 to BeginIndex to read record from the start
'Put 0 to LimitIndex to load all records
Sub ExecuteToList(SQL As SQL, Query As String, FieldToShow, BeginIndex As Int, LimitIndex As Int) As List
    Dim Table As List
    Dim cur As Cursor
    Dim Cols() As String
    cur = SQL.ExecQuery(Query)
    If cur.RowCount <> 0 Then
        Table.Initialize
        If LimitIndex = 0 Then
            For i = BeginIndex To cur.RowCount - 1
                cur.Position = i
                Table.Add(cur.GetString(FieldToShow))
            Next
        Else
            For i = BeginIndex To LimitIndex
                cur.Position = i
                Table.Add(cur.GetString(FieldToShow))
            Next
        End If
        Return Table
    End If
End Sub
 

Attachments

  • DBUtils.bas
    22 KB · Views: 169
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
I see... Can you please alter this sub as follows, and tell us if the error still appears? If all gone well, returning from the sub, you could use its size property to check in any items return and then add items as needed.
B4X:
Sub ExecuteToList(SQL As SQL, Query As String, FieldToShow, BeginIndex As Int, LimitIndex As Int) As List
  Dim Table As List
  table.initialize
   Dim cur As Cursor
  Dim Cols() As String
  cur = SQL.ExecQuery(Query)
  If cur.RowCount > 0 Then
  'Table.Initialize
  If LimitIndex = 0 Then
  For i = BeginIndex To cur.RowCount - 1
  cur.Position = i
  Table.Add(cur.GetString(FieldToShow))
  Next
  Else
  For i = BeginIndex To LimitIndex
  cur.Position = i
  Table.Add(cur.GetString(FieldToShow))
  Next
  End If
  End If
    cur.close
    Return Table
End Sub
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Did you tried
B4X:
If cur.RowCount > 0 Then
Don´t know if this is the problem
The sub is a bit problematic. If no matching rows found, it returns nothing. This is why I altered the code a little. Still, let's see :)
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
Try this:
B4X:
Dim tempList, lst As List
tempList = DBUtils.ExecuteToList(Starter.SQL1, "Select name FROM contacts ORDER BY name", "name", 0, 0)
If (tempList.Size = 0) Then
    ToastMessageShow("There is no names in the list."&CRLF&"Using default values.", False)
    lst.Initialize
    lst.AddAll(Array As String("Name 01","Name 02","Name 03"))
Else
    lst = tempList
End If
 
Upvote 0

asales

Expert
Licensed User
Longtime User
I see... Can you please alter this sub as follows, and tell us if the error still appears? If all gone well, returning from the sub, you could use its size property to check in any items return and then add items as needed.
B4X:
Sub ExecuteToList(SQL As SQL, Query As String, FieldToShow, BeginIndex As Int, LimitIndex As Int) As List
  Dim Table As List
  table.initialize
   Dim cur As Cursor
  Dim Cols() As String
  cur = SQL.ExecQuery(Query)
  If cur.RowCount > 0 Then
  'Table.Initialize
  If LimitIndex = 0 Then
  For i = BeginIndex To cur.RowCount - 1
  cur.Position = i
  Table.Add(cur.GetString(FieldToShow))
  Next
  Else
  For i = BeginIndex To LimitIndex
  cur.Position = i
  Table.Add(cur.GetString(FieldToShow))
  Next
  End If
  End If
    cur.close
    Return Table
End Sub
Thanks @mc73. This code works fine!
Now I use "If (lst.size = 0) Then" to ckecked if list is empty.
 
Upvote 0
Top