-1

Using vb.NET I am trying to pull data from a database via a dataset but when running the code for the button, I get this error:

"An unhandled exception of type 'System.NullReferenceException' occurred in Full Program.exe

Additional information: Object reference not set to an instance of an object."

Here is my code, what is wrong with it?

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

It is still throwing up the same error. This is the new code:

Still not working. This is the new code:

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

    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

End Class

  • That's the kind of thing you could have solved much quicker with a simple debugging session, friend. :) – Crono Mar 12 '14 at 12:13
  • 2
    Step through the code in a debugger. When the exception happens, which object is null (`Nothing` in VB)? Where did you initialize that object? – David Mar 12 '14 at 12:13

2 Answers2

3

Your con connection object has not been instantiated. Put this line in before trying to set the connection string:

con = New OleDb.OleDbConnection()

You are going to have the same problem for ds. It should be instantiated before calling the adapter's Fill method.

ds = New DataSet()

As an additional advice, I'd suggest you remove the second parameter passed to the adapter's Fill method and fetch the DataTable instance this way:

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

And finally, next time, use the debugger to solve this kind of problem. It will be much easier and faster than asking on SO. ;)

Crono
  • 10,211
  • 6
  • 43
  • 75
0

You do not seem to have initialised 'con', thus it will contain null. Initialise it before using it.

con = New OleDbConnection()
ikkentim
  • 1,639
  • 15
  • 30