Android Question Cursor not initilaized

Roger C

Active Member
Licensed User
Longtime User
Hi, I'm banging my head against something soft...

Below code gives me 'Cursor is not initilized'-error. I had it in an Activity before but now I am running this in a Class. Does it matter if it's in an Activity or Class?????

B4X:
Sub FillHistory2(lDate As String)
 
    Dim crnames As Cursor
    Dim rowitem As String
    Dim t As String
   
    lstHistory.Clear
      
    crnames = Main.SQLuser.ExecQuery("SELECT intid, uttid, indatum, projekt FROM time WHERE indatum = '" & lDate & "' ORDER BY intid DESC")
   
    If crnames.RowCount > 0 Then
        For i = 0 To crnames.RowCount - 1
            crnames.Position = i
            If crnames.GetString("indatum")=lDate Then      'DateTime.Date(DateTime.now) Then
                If crnames.GetString("uttid") <> "00:00:00" Then
                    rowitem=sf.left(crnames.GetString("intid"),5) & "-" & sf.Left(crnames.GetString("uttid"),5) & "  " & crnames.GetString("projekt")
                Else
                    rowitem=sf.Left(crnames.GetString("intid"),5) & "-" & " Pågår " & crnames.GetString("projekt")
                End If
                lstHistory.AddSingleLine(rowitem)    'crnames.GetString("Name"))
            End If
        Next
    Else
        lstHistory.AddSingleLine("<Inga projekt denna dag>")
    End If
    crnames.Close
End Sub

The cursor doesn't have an Initilize. What am I doing wrong???
 

barx

Well-Known Member
Licensed User
Longtime User
The cursor is not initialized as no records found. Try putting square brackets around the table name. Time is probably a reserved keyword.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Roger C

Active Member
Licensed User
Longtime User
Aha, ok so I don't need to initalize it somehow just make sure it will find some records...
Maybe I should put it in a Try - Catch....
But the odd thing is that it worked fine when run in an Activity before, when no records found you come to the Else-part...
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
I believe when testing if >0 it tries to access the row.count which it cannot do as the cursur is not initialized. Try wrapping in a If crnames.IsInitialized
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
You'll get MyCursor.IsInitialized = True even if MyCursors.RowCount = 0
yes, but you do if crnames.IsInitialized = false. That way when it is not initialized the rest of the code won't be run. And so no error. Was keeping answers short as on my phone and typing messes up in the reply box
 
Last edited:
Upvote 0

Roger C

Active Member
Licensed User
Longtime User
Thanks for your fast response!
I'm a bit ashamed here... It wasn't cursor, it was SQLuser who wasn't initilized. The B4A said that the problem was at cursor but never mind. Been rearranging some code and then it didn't work. Hrm...:oops:
 
Upvote 0
Top