我想列出具有许多属性的产品,例如价格,名称,id等。
我制作了一个模块并创建了一个struct产品,然后列出了所有产品。
“添加”按钮应将文本字段中的值添加到列表中,但不起作用。出现错误:
“类型为'System.NullReferenceException'的未处理异常 发生在RFID.exe中”
此行出现错误:
Module1.newProduct.Add(product)
这是显示其工作方式并请帮助发现错误的代码。
在模块1中:
Public newProduct As List(Of product)
Structure product
Public productID As Integer
Public productName As String
Public Category As String
Public releaseDate As Date
Public price As Double
Public quantity As Integer
End Structure
在主表单中的添加按钮中:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TxtPID.Text <> "" Then
Dim product As Module1.product
product.productID = Convert.ToInt32(TxtPID.Text)
product.productName = TxtPName.Text
product.Category = LCategory.Text
product.releaseDate = TxtRDate.Text
product.price = Convert.ToDouble(TxtPrice.Text)
product.quantity = Convert.ToInt32(TxtQuantity.Text)
Module1.newProduct.Add(product)
MsgBox("Product has been added.")
Else
MsgBox("Enter Product ID!")
End If
End Sub
答案 0 :(得分:1)
您需要创建对象newProduct
的新实例
Public newProduct As New List(Of product)