Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I really need someones help on this project as the deadline is for tomorrow! (I left it a bit late I know!) Thanks in advance.

I'm trying to code an array in vb.net to save information at a record position from several listboxes into an array in a .dat file. My problem is the data will not save, and I keep getting this error: Object reference not set to an instance of an object at .JuiceID(i)
This is the code for the array:
VB
For i = 0 To lstJuiceID.Items.Count - 1
                With OrderItems
                .JuiceID(i) = lstJuiceID.Items.Item(i)
                .JuiceName(i) = lstJuiceName.Items.Item(i)
                .Quantity(i) = lstQuantity.Items.Item(i)
                .Total(i) = lstTotal.Items.Item(i)
                .UnitPrice(i) = lstUnitPrice.Items.Item(i)
                End With
            OrderItems.CustomerID = lblID.Text
            OrderItems.OrderNumber = lblOrderNumber.Text
        Next
        
        FileOpen(3, "OrderItems.dat", OpenMode.Random, , , Len(OrderItems))
        RecordPosition = LOF(3) / Len(OrderItems)
        RecordPosition = RecordPosition + 1
        FilePut(3, OrderItems, RecordPosition)
        FileClose(3)

        MsgBox("Order Saved!") 'tells the user the order has been saved

and this is the code for the OrderItems Data Structure:
VB
Module OrderItems
    Public Structure OrderItemType
        <VBFixedString(5)> Dim CustomerID As String
        <VBFixedString(6)> Dim OrderNumber As String
        <VBFixedString(6)> Dim JuiceID() As String
        <VBFixedString(20)> Dim JuiceName() As String
        Dim UnitPrice() As String
        Dim Quantity() As String
        Dim Total() As String
    End Structure
End Module

Can you help me? Thanks!
Posted
Updated 15-Oct-14 11:48am
v2
Comments
j snooze 15-Oct-14 17:53pm    
If you look real close OrderItems is not what you should be using in your "with" statement thats the name of the module.
Member 11118898 15-Oct-14 17:56pm    
Sorry, missed that bit out, at the top I delcared OrderItems as OrderItemType
Maciej Los 15-Oct-14 17:56pm    
Are you shure, that:
Dim UnitPrice() As String
Dim Quantity() As String
Dim Total() As String

should be a string variables?
Member 11118898 15-Oct-14 18:00pm    
Yes I set them as string at first, which I probably shouldnt have, then converted them to integer by using CInt when doing calculations.
Maciej Los 15-Oct-14 18:05pm    
I believe, you answered it...

1 solution

Please, read my comment to the question.

You're almost done, try to finish it. Have a look here:
Structures and Other Programming Elements (Visual Basic)[^]
Array of Structure[^]

It should help you to find solution.
 
Share this answer
 
Comments
Member 11118898 15-Oct-14 18:59pm    
I've managed to get the data saved in the array but cant seem to display it. i am I doing it correctly?
For i = 0 To 6
With OrderItems(6)
lstJuiceID.Items.Add(.JuiceID)
lstJuiceName.Items.Add(.JuiceName)
lstQuantity.Items.Add(.Quantity)
lstTotal.Items.Add(.Total)
lstUnitPrice.Items.Add(.UnitPrice)
End With

Next
FileOpen(3, "OrderItems.dat", OpenMode.Random, , , Len(OrderItems))
FileGet(3, OrderItems, OrderRecordPosition)
FileClose(3)
Maciej Los 16-Oct-14 1:54am    
Display... where?
Member 11118898 16-Oct-14 13:37pm    
Sorry I need to be more specific! I'm saving the array using this code: For i = 0 To lstJuiceID.Items.Count - 1
Dim OrderItems(i) As OrderItemType
With OrderItems(i)
.JuiceID = lstJuiceID.Items.Item(i)
.JuiceName = lstJuiceName.Items.Item(i)
.Quantity = lstQuantity.Items.Item(i)
.Total = lstTotal.Items.Item(i)
.UnitPrice = lstUnitPrice.Items.Item(i)
.CustomerID = lblID.Text
.OrderNumber = lblOrderNumber.Text
End With
FileOpen(3, "OrderItems.dat", OpenMode.Random, , , Len(OrderItems(i)))
RecordPosition = LOF(3) / Len(OrderItems(i))
RecordPosition = RecordPosition + 1
FilePut(3, OrderItems(i), RecordPosition)
FileClose(3)
Next
MsgBox("Order Saved!") 'tells the user the order has been saved
And then trying to display the array on a different form using this code: FileOpen(3, "OrderItems.dat", OpenMode.Random, , , Len(OrderItems))
Do While Not EOF(3)
FileGet(3, OrderItems, OrderRecordPosition)
lstJuiceID.Items.Add(OrderItems.JuiceID)
lstJuiceName.Items.Add(OrderItems.JuiceName)
lstQuantity.Items.Add(OrderItems.Quantity)
lstTotal.Items.Add(OrderItems.Total)
lstUnitPrice.Items.Add(OrderItems.UnitPrice)
Loop
FileClose(3)

But my problem now is that its only displaying one item from the array?
Maciej Los 16-Oct-14 15:18pm    
Please, improve your question. Use "Improve question" widget. The code you provided in comment is unreadible. Do not forget to use proper formating.
On the first look... It looks like there is a problem with writing and reading data to/from *.dat file. Have you checked the file (via notepad, for example)?
Member 11118898 17-Oct-14 19:18pm    
Hi Sorry for the late reply, I managed to get it working! I got the data saving to the array in the .dat file, but when trying to view the array I had coded it wrong, and the program was looking in the wrong record position for the data so that's why it wasn't displaying! Thanks again for your help :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900