Mary Dubois

Mary Dubois

  • NA
  • 3
  • 1.7k

NullReferenceException was unhandled

Mar 3 2013 9:56 AM
Hi,

I am getting a "NullReferenceException was unhandled - Object Reference not set to an instance of an object" with the following code:

Public Class Enter_Pin

    ''Declaring Variables
    Dim inc As Integer
    Dim con As New OleDb.OleDbConnection
    Dim dbProvider As String
    Dim dbSource As String
    Dim ds As New DataSet
    Dim da As New OleDb.OleDbDataAdapter
    Dim sql As String
    Dim maxrows As Integer

    Private Sub cmdEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEnter.Click

        ''entering Pin submission
        Dim searchID, i, CurrentRow As Integer
        searchID = txtPin.Text
        i = 0
        While i <> maxrows + 1

            ''this is where I am getting the error
                If searchID = ds.Tables("AddressBook").Rows(i)("Pin") Then
                Me.Close()
                Customer_Menu.Show()
            ElseIf i = maxrows Then
                MsgBox("Invalid Pin Number")
                Exit While
            End If
            i += 1
        End While
        CurrentRow = i
    End Sub

    Private Sub Enter_Pin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ''Searching the database to find the correct pin number / database connection
        dbProvider = "Provider=Microsoft.Jet.4.0;"
        dbSource = "Data Source = C:\Database\AddressBook.mdb;"
        con.ConnectionString = dbProvider & dbSource
        con.Open()
        sql = "SELECT * FROM AddressBook"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "AddressBook")
        maxrows = ds.Tables("AddressBook").Rows.Count

    End Sub

    Private Sub cmdOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOne.Click, cmdTwo.Click, cmdThree.Click, cmdFour.Click, cmdFive.Click, cmdSix.Click, cmdSeven.Click, cmdEight.Click, cmdNine.Click, cmdZero.Click
        ''clicking on the pin number buttons
        txtPin.Text = txtPin.Text & sender.text
    End Sub

    Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click
        ''pin clears from the textbox
        txtPin.Text = ""
    End Sub

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        ''if cancel clicked on, taken back to Welcome Screen
        Welcome_Screen.Show()
        Me.Close()
    End Sub

End Class

If someone could have a look over this and check it out for me, this would be greatly appreciated. I am currently building an ATM program for college and this is the first issue I have came across. The online help isn't the greatest, so any help would be appreciated. Using VB 2008 for this.

Regards

Mary

Answers (1)