gpt4 book ai didi

mysql - 尝试从 MySQL 读取时出现 "Object Reference not Set to an Instance of an Object"

转载 作者:行者123 更新时间:2023-11-29 13:05:17 27 4
gpt4 key购买 nike

自从我们几个小时前收到这项作业以来,我一直在努力完成明天到期的作业。我们的类(class)从未真正教授过 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

这就是我所知道的有关该错误的全部信息。我一直在寻找某种堆栈跟踪,但我发现的只是这个无用的文本 block ,它有点太乱了,无法粘贴到这里,所以在这里:http://pastebin.com/nGqw6V8R

最佳答案

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

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

关于mysql - 尝试从 MySQL 读取时出现 "Object Reference not Set to an Instance of an Object",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22800141/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com