Reputation: 5
I'm working on an assignment for my class where we learn to use dictionaries. I have to save the order objects to the dictionary. When I debug it gives me the NullReferenceException The orders are being created correctly, just won't be assigned to the dictionary, and then I have to display the orders to a listbox using the dictionary
Public Class MainForm
Public Shared orders As Dictionary(Of Integer, Order)
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim aOrder As Order
'order created for Joe Jones
aOrder = New [Order](#05/1/2017#, "Joe Jones")
Dim sillyPuttyItem As New Item("silly putty", 3.95, 10, 2.99)
Dim rubiksItem As New Item("Rubik's Cube", 9.1)
aOrder.AddItem(sillyPuttyItem)
aOrder.AddItem(rubiksItem)
orders.Add(1, aOrder)
aOrder = New [Order](#12/5/2017#, "Kathy Klum")
Dim fidgetItem As New Item("Fidget Spinner", 6.79, 5, 5.98)
Dim bubblesItem As New Item("bottle of bubbles", 0.99)
aOrder.AddItem(fidgetItem)
aOrder.AddItem(bubblesItem)
Order.orders.Add(2, aOrder)
For Each order In orders
lstOrders.Items.Add(orders.ToString())
Option Strict On
Option Explicit On
Public Class Order
Public Shared orders As Dictionary(Of Integer, Order)
Public Shared NEXT_ORDER_NUMBER As Integer = 1234
Private Const SALES_TAX_RATE As Double = 0.08
Private mOrderDate As DateTime
Private mOrderNumber As Integer
Private mName As String
Private mQuantityOrdered As Integer
Private mItems As New List(Of Item)
Public Sub New(ByVal pDate As DateTime, ByVal pName As String)
mOrderDate = pDate
mName = pName
End Sub
Public ReadOnly Property OrderDate As DateTime
Get
Return mOrderDate
End Get
End Property
Public ReadOnly Property OrderNumber As Integer
Get
Return mOrderNumber
End Get
End Property
Public ReadOnly Property Name As String
Get
Return mName
End Get
End Property
Next
Option Strict On
Option Explicit On
Public Class Item
Private itemName As String
Private itemPrice As Double
Private bulkQuantity As Integer
Private bulkPrice As Double
Private mquantityOrdered As Integer
Public ReadOnly Property QuantityOrdered As Integer
Get
Return mQuantityOrdered
End Get
End Property
Public Sub New(ByVal pItemName As String, ByVal pPrice As Double)
itemName = pItemName
itemPrice = pPrice
' mquantityOrdered = pQuantity
End Sub
Public Sub New(ByVal pItemName As String, ByVal pPrice As Double,
ByVal pBulkQuantity As Integer, ByVal pBulkPrice As Double)
bulkQuantity = pBulkQuantity
bulkPrice = pBulkPrice
' mquantityOrdered = pQuantity
End Sub
Public Sub New(ByVal pItem As Item)
itemName = pItem.Name
itemPrice = pItem.itemPrice
bulkQuantity = pItem.bulkQuantity
bulkPrice = pItem.bulkPrice
End Sub
Public Sub New(ByVal pItem As Item, ByVal pQuantity As Integer)
Me.New(pItem)
mquantityOrdered = pQuantity
End Sub
Upvotes: 0
Views: 83
Reputation: 141668
This
Public Shared orders As Dictionary(Of Integer, Order)
Declares the shared field orders
of type Dictionary(Of Integer, Order)
. It doesn't actually create it. I think you are looking for:
Public Shared orders As New Dictionary(Of Integer, Order)
Notice the "New" in there.
I'm now getting the exception when adding the second order to the dictionary, but it takes the first one
Your second add is adding to different dictionary. The first one is doing:
orders.Add(1, aOrder)
and the second is doing:
Order.orders.Add(2, aOrder)
The Order
class seems to have another shared Dictionary that is also Nothing. You should either Use New Dictionary
for the Dictionary in Orders, or perhaps you meant to do orders.Add(2, aOrder)
and not use the shared field in Orders
.
Upvotes: 1