Is it possible to get an empty reference error from the ListBox.Items property?

EDIT 11-20-2009 . This question was posted quite a while ago, but the problem just crept up this morning; so I hope someone else can provide some insight (although the answers provided were already helpful).

As soon as the moon is blue in our production environment, we get NullReferenceException

the property reference from the Items

control ListBox

. I have added the example code below.

The parent form ListBox

contains a private Queue<string>

called QueuedMessages

. This queue receives new event messages. With a timer that goes off every 500ms, the following method is executed:

void DisplayQueuedMessages() {
    lock (QueuedMessages) {
        while (QueuedMessages.Count > 0) {
            string msg = QueuedMessages.Dequeue();
            this.lbxMessages.Items.Insert(0, msg); // NullReferenceException
            if (this.lbxMessages.Items.Count > MAX_LBX_ITEMS) {
                this.lbxMessages.Items.RemoveAt(Me.lbxMessages.Items.Length - 1);
            }
        }
    }
}

      

Again, as I mentioned, this very rarely throws NullReferenceException

. After a few months of using the app, this happened three or four times.

Also, a few times this has happened, it seems that either the property ListBox.Items

or just ListBox

itself mysteriously gone forever: all subsequent methods that add elements to throw exceptions ListBox

. The only way to recover is to close the app and bring it back.

Unfortunately, constantly distracted by a million other things, I never intended to add registrations before inserts. I've added registration now, but it might be a month or more before we see this issue again. At the same time, more ideas? What are the possible explanations for this?

I think my real question is: Has anyone else ever seen this - access ListBox

that actually existed and suddenly got it NullReferenceException

- and could you ever figure out why and how to fix the problem?

+2


source to share


4 answers


Take a deep look at the whole code, it is very likely that Me.ListBox1 is null.

I was once working on an application created by a third party and this code was only called in a specific case and threw an exception while rendering the asp.net code ...



void ClearItems()
{
   SomeField.Text = "";
   ...
   AnotherField = null; 
   ...
}

      

Also note the stack trace in other failures, it would be different if it exploded inside a control method than if it exploded in your code.

+2


source


How sure are you that you Me.ListBox1

did not go to zero? This would be my first guess.



+1


source


Set a conditional breakpoint first and try to figure out and / or write security code. I would add something like tis before your code:

System.Diagnostics.Debug.Assert(Me.ListBox1 != null);
System.Diagnostics.Debug.Assert(Me.ListBox1.Items != null);
String msg = getStatusMessage(); 
System.Diagnostics.Debug.Assert(msg != null);
Me.ListBox1.Items.Insert(0, msg);

      

(I Gues I'm mixing VB and C # here, but you get the image.)

Also make sure the null exception does not actually occur in the SelectedIndexChanged event or similar.

But it is possible that this is indeed a ListBox problem, see this question .

+1


source


The reason is the System.Windows.Form v2.0 error that I identified this morning (and this is fixed in System.Windows.Form v4.0).

For me, this happened when my code adds Item

to ListBox

while the hosting process finishes. The private instance field ListBox.listItemsArray

is null and this calls NullReferenceException

. I don't know exactly why ListBox.listItemsArray

it is null, but I think it has something to do with a descriptor creation problem.

For me, the workaround was simple, as a try/catch

was enough because the process is closed anyway. You can of course sort out the problem of decompiling the Reflector function like I did:

enter image description here

+1


source







All Articles