user1667096
user1667096

Reputation:

"Object Reference not Set to an Instance of an Object" when trying to read from MySQL

I've been trying to finish up this assignment, due tomorrow, since we got it a few hours ago. Our class was never really taught VB, we just did a brief overview of the very basics of the language and then were told to go copy the code on our assignments and fill in the rest.

At the moment I'm stuck with an error, which you can see in the title, which is preventing me from finishing up the rest of my program. I've looked around for solutions, but they haven't been very helpful due to my complete lack of knowledge of VB and Visual Studio. (I normally code in Java with Eclipse.)

If anyone can explain what's causing this error and how to avoid it in the future that would help a lot. Thanks for any replies.

Imports System.Data
Imports MySql.Data.MySqlClient

Public Class inventoryUpdate_form
Dim quantity As Integer
Dim quantityList As List(Of Integer)
Dim isbn As Integer
Dim isbnList As List(Of Integer)

Public Sub refreshData(first As Boolean)
    'Establish connection to the DB.
    Dim connection As MySqlConnection = New MySqlConnection
    connection.ConnectionString = "server=ERASED;port=ERASED;user id=ERASED;password=ERASED;database=ERASED"

    Try
        connection.Open()

        ' Clear the quantity and isbn lists before adding new data.
        Try
            quantityList.Clear()
        Catch e As NullReferenceException
        End Try

        Try
            isbnList.Clear()
        Catch e As NullReferenceException
        End Try


        ' Build a query
        Dim query As String
        query = "SELECT book.title, inventory.quantity, book.ISBN FROM book JOIN inventory ON book.ISBN = inventory.ISBN JOIN store ON inventory.store_id = store.store_id WHERE store.city = 'Fredericton' AND inventory.quantity > 0 ORDER BY book.title ASC"

        ' Run the query.
        Dim cmd As New MySqlCommand(query, connection)
        Try
            Dim dataReader As MySqlDataReader = cmd.ExecuteReader
            Dim title As String

            While dataReader.Read()
                title = dataReader("title")
                ComboBox1.Items.Add(title)
                quantity = dataReader("quantity")
                quantityList.Add(quantity)
                isbn = dataReader("ISBN")
                isbnList.Add(isbn)
            End While

            dataReader.Close()
        Catch e As Exception
            MessageBox.Show("Data Reader error: " & e.Message)
        End Try
    Catch e As MySqlException
        ' If an error occurs while connecting to the DB then show the error message.
        MessageBox.Show("Cannot connect to the database: " & e.Message)
    Finally
        ' Close and dispose of the connection to the DB.
        connection.Close()
        connection.Dispose()
    End Try
End Sub

Private Sub inventoryUpdate_form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    refreshData(True)
End Sub
End Class

The error is being shown on this try-catch:

' Run the query.
Dim cmd As New MySqlCommand(query, connection)
Try
    Dim dataReader As MySqlDataReader = cmd.ExecuteReader
    Dim title As String

    While dataReader.Read()
        title = dataReader("title")
        ComboBox1.Items.Add(title)
        quantity = dataReader("quantity")
        quantityList.Add(quantity)
        isbn = dataReader("ISBN")
        isbnList.Add(isbn)
    End While

    dataReader.Close()
Catch e As Exception
    MessageBox.Show("Data Reader error: " & e.Message)
End Try

That's all I know about the error. I've been looking around for some sort of stack trace, but all I've found is this unhelpful block of text which is a bit too messy to paste here so here you go: http://pastebin.com/nGqw6V8R

Upvotes: 0

Views: 1353

Answers (2)

pinkyjong
pinkyjong

Reputation: 1

I'm not sure but if you are getting a "Object Reference..." error in the try catch block you mention it could be the sibList is not inizialized

Try add in the declaration of the variable this:

Dim isbnList As New List(Of Integer)

Upvotes: 0

Anthony Chu
Anthony Chu

Reputation: 37520

quantityList and isbnList are null. Use New to initialize them...

Dim quantityList As New List(Of Integer)
Dim isbnList As New List(Of Integer)

Upvotes: 1

Related Questions