0

Can you see what I did wrong in this code? I'm using DBUtils to populate some text boxes but I get a "Null pointer exception" on the line with the "Get". I also used a Toast to make sure that intCurrentId actually did have a value.

Here's the code I used:

Sub ListViewPeopleEventHandler_ItemClick (Position As Int, Value As Object)

  ' Update the details area.
  '-------------------------
  Dim valuesFromTheListView() As String
  valuesFromTheListView = Value

  intCurrentId = valuesFromTheListView(0)

  Dim mapOfTableFields As Map
  mapOfTableFields = DBUtils.ExecuteMap(SQL, _
          "SELECT Id, FirstName, LastName FROM PeopleToVisit WHERE id = ?", _
          Array As String(intCurrentId))

  ToastMessageShow(intCurrentId, False) 

  ' I get the error on this next line.
  '-------------------------------------
  EditTextFirstName.Text = mapOfTableFields.Get("FirstName")

  EditTextFirstName.RequestFocus
  EditTextFirstName.SelectAll
  EditTextFirstName.Color = Colors.Cyan

  EditTextLastName.Text = mapOfTableFields.Get("LastName")
  EditTextLastName.RequestFocus
  EditTextLastName.Color = Colors.Cyan

  tableMode = "Edit"
  Activity.Title = "Maintenance - Result Of Visit *** EDIT ***"
End Sub

This is the structure of the database table:

SQL.ExecNonQuery("CREATE TABLE PeopleToVisit (" & _
                 "Id INTEGER PRIMARY KEY, " & _
                 "FirstName TEXT, " & _
                 "LastName TEXT, " & _
                 "Address1 TEXT, " & _
                 "Address2 TEXT, " & _
                 "City TEXT, " & _
                 "State TEXT, " & _
                 "Zip TEXT, " & _
                 "PrimaryPhone TEXT, " & _
                 "SecondaryPhone TEXT, " & _
                 "Email TEXT, " & _
                 "LastVisitNote TEXT " & _
                 ")")

Thanks.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Emad-ud-deen
  • 4,734
  • 21
  • 87
  • 152
  • You said you checked to make sure `intCurrentID` actually had a value, but did you check to see if there was a row in the DB for that ID? (Also, please check your code formatting in the preview window below your posts as you write them; it's much easier if you format so that there's no side-to-side scrolling needed to read it.) – Ken White Nov 25 '11 at 02:19
  • Hi Ken, It should be there because the id comes from the value that was in the list view so I'm using that Id to do the query. The intended purpose of this sub routine is to use the value from the list view and do a query to obtain the rest of the details in the database table so they can be shown in the other text boxes. – Emad-ud-deen Nov 25 '11 at 02:38
  • I know it *should* be. :) But did you verify that it *is*? In other words, did you make sure that not only does `intCurrentID` have a value, but that it's a **valid** value? If you've inserted 3 people in the database, and the `intCurrentID` value is 5, it could be important. :) – Ken White Nov 25 '11 at 02:43
  • Currently there are 2 rows of data in the table and the toast will show either intCurrentID = 1 or intCurrentID = 2. I also tried ToastMessageShow(mapOfTableFields.Get("Id"), False) and it always returned "Null". – Emad-ud-deen Nov 25 '11 at 09:55

1 Answers1

0

I'm not sure why the DBUtils version did not work but when I used a cursor as in this revised version of the sub procedure, everything worked ok.

Sub ListViewPeopleEventHandler_ItemClick (Position As Int, Value As Object)

    ' Update the details area.
    '-------------------------
    Dim valuesFromTheListView() As String
    Dim oRecordSet As Cursor

    valuesFromTheListView = Value   
    intCurrentId = valuesFromTheListView(0)

    oRecordSet = SQL.ExecQuery( _
        "SELECT Id, FirstName, LastName " & _
          "FROM PeopleToVisit " & _
         "WHERE id = " & intCurrentId)

    If oRecordSet.RowCount > 0 Then
        oRecordSet.Position = 0

        EditTextFirstName.Text = oRecordSet.GetString("FirstName")
        EditTextLastName.Text = oRecordSet.GetString("LastName")
    End If

    EditTextFirstName.RequestFocus
    EditTextFirstName.SelectAll
    EditTextFirstName.Color = Colors.Cyan

    EditTextLastName.RequestFocus
    EditTextLastName.Color = Colors.Cyan

    tableMode = "Edit"
    Activity.Title = "Maintenance - Result Of Visit *** EDIT ***"
End Sub
Emad-ud-deen
  • 4,734
  • 21
  • 87
  • 152