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
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
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
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
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