-1

I want to do a list of products wich has many properties such as price, name, id,..etc.

I made a module and create a struct product then I made a list to have all products.

The add button should add values from textfields to the list however it doesn't work. Getting error:

"An unhandled exception of type 'System.NullReferenceException' occurred in RFID.exe"

I am getting the error in this line:

Module1.newProduct.Add(product)

This is the code showing how it done and help find the mistake please.

In module1:

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

In the main form, in the add button:

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
ɐsɹǝʌ ǝɔıʌ
  • 4,440
  • 3
  • 35
  • 56
AL3MS
  • 91
  • 3
  • 9
  • Why you don't use classes? Your `Product` is clearly one. – Tim Schmelter Nov 28 '18 at 08:35
  • I don't know the number of products, it should be added by user. Does class works in this situation? and how? I know how to create a class and how to create objects but I don't have any idea about how does it work with as many as user inputs. @Rango – AL3MS Nov 28 '18 at 09:10
  • 2
    Sure, classes work as structures. The difference is that structures are value types while classes are reference types. Use structures for types that are small(few properties) and kind of measurable/calculable. By default use classes – Tim Schmelter Nov 28 '18 at 09:16

1 Answers1

1

You need to create a new instance of your object newProduct

Public newProduct As New List(Of product)
ɐsɹǝʌ ǝɔıʌ
  • 4,440
  • 3
  • 35
  • 56