2011-11-25 58 views
0

你能看見我做錯了什麼在此代碼?我正在使用DBUtils來填充一些文本框,但我在「Get」行上得到了一個「空指針異常」。我還使用Toast來確保intCurrentId實際上有一個值。使用空指針異常DBUtils

這是我使用的代碼:

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 

這是數據庫表的結構:

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 " & _ 
       ")") 

感謝。

+0

你說你檢查確保'intCurrentID'實際上有一個值,但你檢查是否有一個在該數據庫中的行該ID? (另外,請檢查你的代碼在您的帖子下方的預覽窗口格式爲你寫他們。這是容易得多,如果你的格式,以便有閱讀它不需要一側到另一側滾動) –

+0

嗨肯,它應該有因爲id來自列表視圖中的值,所以我使用該Id來執行查詢。該子例程的預期目的是從列表視圖中使用的價值,做一個查詢,以獲取數據庫中的表其餘的細節,使他們能夠在其他文本框中顯示。 –

+0

我知道它*應該*。 :)但是你確認它*是*嗎?換句話說,你確保不僅'intCurrentID'具有價值,但它是一個** **有效價值?如果你已經在數據庫中插入了3個人,並且'intCurrentID'值爲5,這可能很重要。 :) –

回答

0

我不知道爲什麼DBUtils版本沒有工作,但是當我使用光標作爲子過程的這個修訂版,一切工作正常。

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