尝试从MySQL读取时,“对象引用未设置为对象的实例”

时间:2014-04-02 01:56:52

标签: mysql vb.net

自从我们几个小时前拿到这份作品以来,我一直试图完成这项任务。我们的课程从未真正教过VB,我们只是对语言的基础知识进行了简要的概述,然后被告知要复制我们的作业代码并填写其余部分。

目前我遇到了一个错误,你可以在标题中看到,这使我无法完成剩余的程序。我已经四处寻找解决方案,但由于我完全缺乏VB和Visual Studio的知识,因此我们并没有提供帮助。 (我通常使用Eclipse编写Java代码。)

如果有人能解释导致此错误的原因以及将来如何避免这种错误会有所帮助。感谢您的回复。

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

此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

我对错误的全部了解。我一直在寻找某种堆栈跟踪,但我发现的所有这些无用的文本块都有点太乱了,无法粘贴到这里,所以你去:http://pastebin.com/nGqw6V8R

2 个答案:

答案 0 :(得分:1)

quantityListisbnList为空。使用New初始化它们......

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

答案 1 :(得分:0)

我不确定,但是如果你得到一个"对象参考..."您提到的try catch块中的错误可能是sibList未被初始化

尝试添加变量声明:

Dim isbnList As New List(Of Integer)