Jason Clark's Blog

Software, Hardware, whatever...

Wierd windows forms ListView Issue, bug in .NET?

While working on a decent sized Windows Forms app in c#, I've seen a sporadic issue crop up that is incredibly difficult to debug.   The form simply has a listview on it that is populated from a worker thread.   The calls are all done with Invoke, on the UI thread.   But, every once and awhile (it's sporadic). .NET throws this:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Windows.Forms.ListViewItemCollection.get_Item(Int32 displayIndex)
  at System.Windows.Forms.ListView.OnHandleDestroyed(EventArgs e)
   at System.Windows.Forms.Control.WmDestroy(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ListView.WndProc(Message& m)
   at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.RunDialog(Form form)
   at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
   at System.Windows.Forms.Form.ShowDialog()

I can't find out where this is coming from, it has no reference to anything in the app, it's all in the framework. 

Anyone seen this?

Comments

manishgour said:

# May 3, 2006 2:29 AM

Frank Wu said:

VS 2005 lets you see the .NET Framework source code.
# May 4, 2006 1:25 PM

Xanax said:

Hi, all!And sopaleness in a man, or darkness, does not make one, nor is there adifference in species between the pale man and the dark man, noteven if each of them be denoted by one word.
# May 16, 2006 9:00 AM

xanax said:

Hi. Therefore allthings will be of necessity, and chance and the possibility of athing's either occurring or not occurring are removed entirely fromthe range of events.
# May 20, 2006 3:35 AM

xanax said:

Nice design. Such are the heavenly bodies; for these do notappear to be now of one nature and again of another, but aremanifestly always the same and share in no change.
# May 20, 2006 3:35 AM

xanax said:

Cool design, but very light. 2 (which cannot be many,for if it were many, 1 would be few), there must be also one whichis absolutely many, e.g.
# May 20, 2006 3:35 AM

xanax said:

Hi. medicine is a relative term because itsgenus, science, is thought to be a relative term.
# May 20, 2006 3:35 AM

xanax said:

Hi. 2 But evidently there is a first principle, and the causes of thingsare neither an infinite series nor infinitely various in kind.
# May 20, 2006 3:36 AM

xanax said:

Nice site. But it is possible to describe each thing notonly by the account of itself, but also by that of something else.This may be done altogether falsely indeed, but there is also a way inwhich it may be done truly; e.g.
# May 20, 2006 3:36 AM

xanax said:

Cool design, but very light. Health,disease, body; the moving cause is the medical art.
# May 20, 2006 3:36 AM

xanax said:

Good site. All the causes now mentioned fall under four senses which arethe most obvious.
# May 20, 2006 3:36 AM

xanax said:

Nice design. Again, since the 1-itself isfirst, and then there is a particular 1 which is first among theothers and next after the 1-itself, and again a third which is nextafter the second and next but one after the first 1,-so the units mustbe prior to the numbers after which they are named when we count them;e.g.
# May 20, 2006 3:36 AM

Nery said:

I came across with the same problem. In my specific case was that I drew a ListView Control onto a TabControl, causing me this same Exception.

I just deleted the TabControl, place the ListView onto the form and voila! It worked!

# May 25, 2007 6:17 PM

mling said:

I have this same problem with my application which has a listview on a winform. This form is refreshed by a timer of one second. Sometimes it throw the  exception above. Do somebody know the solution for this?

# July 26, 2007 8:45 AM

Ken said:

This may occur if the form is closed and then the background thread starts an update.

You may get a related error if the background thread starts to update the listview before the handle for the form has been created (i.e. a very short time after the form's constructor runs). To prevent this, ensure the handle is created in the form's constructor by referencing it:

IntPtr x = this.Handle; // this is sufficient.

# February 25, 2009 12:44 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)