0

I'm writing code and there is a null reference exception. I can see where it is, but I can't work out how to fix it. I'm honestly not sure why, I've been reading and re-reading my code, trying different things. I think I'm being dense or possibly have a mental block on it somehow.

The two bits of code are :

       For looper = 0 To 3

        Sandpit.Controls.Add(Me.QueueMembers(looper).VisRep)
        Me.QueueMembers(looper).VisRep.ForeColor = Me.QueueMembers(looper).GetBackgroundColour
        Select Case (Me.Direction)
            Case "R"
                TempPoint.X += 32
                TempPoint.Y = 260
            Case "U"
                TempPoint.Y += 32
            Case "L"
                TempPoint.X -= 32
        End Select

Which works for creating a queue and everything. When I try to remove anything from the queue, though, using the following code:

       Public Sub RemoveDiner()
    Dim TempDiner As Diner
    For looper = 0 To Me.EndPointer - 1
        TempDiner = QueueMembers(looper)
        QueueMembers(looper) = QueueMembers(looper + 1)

    Next           
        DrawQueue()
        End Sub

It's the looper causing the problem, I think because once the item is deleted, there aren't three items in the QueueMembers array. If someone could just give me advice on how to fix this because I'm being completely dense about it, please, I would be very grateful. Thanks!

  • Okay, I've now managed to get it to work for 3 items being removed by using a new variable called looperNumber which decreases by 1 at the end of RemoveDiner. And has 3 to start with, for when items are added to the queue. But it doesn't work for more than 3 items, and I need to use 3 for drawQueue for when I add items to the queue...any help? – christinesangel100 Mar 02 '15 at 09:50
  • What line is causing the null reference xception? From what I can tell you're not actually deleting a member of the QueueMembers array, just overwriting one. It might be that `looper + 1` is calling a member of the array that doesn't exist (QueueMembers(3) for example) – Phil Mar 02 '15 at 09:53
  • Sandpit.Controls.Add(Me.QueueMembers(looper).VisRep) That's the line I got the error message one. – christinesangel100 Mar 02 '15 at 10:09
  • And yeah, I'm not deleting it, but just removing it/overwriting it. I'm planning to feed the items of one queue into another, but I'm just trying to get this working first as well. – christinesangel100 Mar 02 '15 at 10:15
  • If you add a break point to that line and step through your code you should be able to find out what exactly is null, i.e. is it Sandpit.Controls or Me.QueueMembers, or Me.QueueMembers(looper)? – Phil Mar 02 '15 at 10:25
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Mar 02 '15 at 13:25

0 Answers0