抛出NullReferenceException

时间:2014-03-12 12:09:01

标签: vb.net ado.net

使用vb.NET我试图通过数据集从数据库中提取数据,但是当运行按钮的代码时,我收到此错误:

“Full Program.exe中发生了'System.NullReferenceException'类型的未处理异常

附加信息:对象引用未设置为对象的实例。“

这是我的代码,它有什么问题?

Public Class mod_data
    Dim inc As Integer
    Dim con As OleDb.OleDbConnection
    Dim dbProvider As String
    Dim dbSource As String
    Dim ds As DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String
    Dim MaxRows As Integer

    Private Sub mod_data_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        dbProvider = "PROVIDER=Microsoft.ACE.12.0;"
        dbSource = "\\fin01\JBU Visual Basic ICT\Ben\Full Program\Full Program\Component_Database.accdb;"
        con.ConnectionString = dbProvider & dbSource

        con.Open()

        sql = "SELECT * FROM tblComponent_List"

        da = New OleDb.OleDbDataAdapter(sql, con)

        da.Fill(ds, "Component_List")

        con.Close()

        MaxRows = ds.Tables("Component_List").Rows.Count

        inc = -1

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        txtComponent_ID.Text = ds.Tables("Component_List").Rows(1).Item(0)
        txtComponent_Name.Text = ds.Tables("Component_List").Rows(1).Item(1)
        '(...)
    End Sub

'(...)

End Sub

@Crono

它仍然会抛出同样的错误。这是新代码:

仍然无法正常工作。这是新代码:

公共类mod_data

Dim inc As Integer

Dim con As OleDb.OleDbConnection

Dim dbProvider As String

Dim dbSource As String

Dim ds As DataSet

Dim da As OleDb.OleDbDataAdapter

Dim sql As String

Dim MaxRows As Integer

Private Sub mod_data_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    ds = New DataSet("Component_DatabaseDataSet3")

    con = New OleDb.OleDbConnection("\\fin01\JBU Visual Basic ICT\Ben\Full Program\Full Program\Component_Database.accdb")

    dbProvider = "PROVIDER=Microsoft.ACE.12.0;"

    dbSource = "\\fin01\JBU Visual Basic ICT\Ben\Full Program\Full Program\Component_Database.accdb;"

    con.ConnectionString = dbProvider & dbSource

    con.Open()

    sql = "SELECT * FROM tblComponent_List"

    da = New OleDb.OleDbDataAdapter(sql, con)

    da.Fill(ds, "Component_List")

    con.Close()

    MaxRows = ds.Tables("Component_List").Rows.Count

    inc = -1

End Sub

Private Sub NavigateRecords()

    txtComponent_ID.Text = ds.Tables("Component_List").Rows(inc).Item(0)

    txtComponent_Name.Text = ds.Tables("Component_List").Rows(inc).Item(1)

End Sub

Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click

    If inc <> MaxRows - 1 Then

        inc = inc + 1

        NavigateRecords()

    Else

        MsgBox("No More Rows")

    End If

End Sub

Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click

    If inc > 0 Then

        inc = inc - 1

        NavigateRecords()

    ElseIf inc = -1 Then

        MsgBox("No Records Yet")

    ElseIf inc = 0 Then

        MsgBox("First Record")

    End If

End Sub

Private Sub btnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click

    If inc <> MaxRows - 1 Then

        inc = MaxRows - 1

        NavigateRecords()

    End If

End Sub

Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click

    If inc <> 0 Then

        inc = 0

        NavigateRecords()

    End If

End Sub

Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click

    ds.Tables("Component_List").Rows(inc).Item(0) = txtComponent_ID.Text

    ds.Tables("Component_List").Rows(inc).Item(1) = txtComponent_Name.Text

    MsgBox("Data updated")

End Sub

Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click

    Dim cb As New OleDb.OleDbCommandBuilder(da)

    ds.Tables("Component_List").Rows(inc).Delete()

    MaxRows = MaxRows - 1

    inc = 0

    da.Update(ds, "Component_List")

    NavigateRecords()

    If MessageBox.Show("Do you really want to Delete this Record?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then

        MsgBox("Operation Cancelled")

        Exit Sub

    End If

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    txtComponent_ID.Text = ds.Tables("Component_List").Rows(1).Item(0)

    txtComponent_Name.Text = ds.Tables("Component_List").Rows(1).Item(1)

End Sub

结束班

2 个答案:

答案 0 :(得分:3)

您的con连接对象尚未实例化。在尝试设置连接字符串之前放入此行:

con = New OleDb.OleDbConnection()

ds您将遇到同样的问题。它应该在调用适配器的Fill方法之前进行实例化。

ds = New DataSet()

作为补充建议,我建议您删除传递给适配器的Fill方法的第二个参数,并以这种方式获取DataTable实例:

MaxRows = ds.Tables(0).Rows.Count

最后,下次使用调试器来解决这类问题。它会比询问SO更容易,更快捷。 ;)

答案 1 :(得分:0)

您似乎没有初始化&#39; con&#39;因此它将包含null。 在使用之前将其初始化。

con = New OleDbConnection()