Entwickler-Ecke

Basistechnologien - NullReference Exception in System.Windows.Forms.DataGridView


IsNull - Mo 18.10.10 17:27
Titel: NullReference Exception in System.Windows.Forms.DataGridView
Hallo,

Ich habe ein Editor-Form, das mit ShowDialog angezeigt wird. Auf diesem Form ist ein UserControl (das scheint aber laut separierten Test problemlos zu laufen), welches ein DataGridView (DGV) hat.

Dieses DGV hat eine Column mit Checkboxen, und wenn ich auf eine solche Column die "Leertaste" drücke (um den Checkbox Status zu toggeln) kriege ich ne Exception; und zwar genau auf:

myform.ShowDialoge(); <----*

d.h. Ich hab eigentlich keinen Plan wo es knallt, wenn ich durchtrace lande ich irgendwann ohne nennenswerten Ereignisse bei der Exception, nachdem alle Internen Methoden abgearbeitet wurden.

Hier der StackTrace:

Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.DataGridViewCheckBoxCell.NotifyMASSClient(Point position)
       at System.Windows.Forms.DataGridViewCheckBoxCell.OnKeyUp(KeyEventArgs e, Int32 rowIndex)
       at System.Windows.Forms.DataGridView.OnKeyUp(KeyEventArgs e)
       at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
       at System.Windows.Forms.DataGridView.ProcessKeyEventArgs(Message& m)
       at System.Windows.Forms.Control.ProcessKeyMessage(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.DataGridView.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.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.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.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()
       at BendBiegeFormView.Editing.Editor.DoEditForm() in C:\*\BendBiegeFormView\Editing\Editor.cs:line 223
       at BendBiegeFormView.Editing.Editor.New() in C:\*\BendBiegeFormView\Editing\Editor.cs:line 109
       at TestForm.Form1.button1_Click(Object sender, EventArgs e) in C:\*\TestForm\Form1.cs:line 38
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.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.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at TestForm.Program.Main() in C:\*\TestForm\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:



Ich kann die Exception nichtmal ignorieren, da ich dadurch aus dem Fenster fliege, und das ist bei einem Editor wohl nicht ganz Userfreundlich.

Kann sich aus dem gegebenen hier einer von euch nen Reim daraus machen? Ich habe nun gut 5h damit verbracht, Debuggt, Intellitrace usw genutzt, aber es scheint irgendwo intern in "System.Windows.Forms" in {System.Collections.ListDictionaryInternal} zu knallen...

any idea?


huuuuuh - Mo 18.10.10 17:42

welchen wert hat myform denn, wenn der fehler auftritt?


IsNull - Di 19.10.10 07:18

myform ist eine Instanz meines Formulars - wie darf ich deine Frage genau verstehen?


Kha - Di 19.10.10 12:47

Autsch, Bugs im Framework sind natürlich immer böse. Hast du mit dem Framework Source Stepping mal in der Methode selbst nach der null-Stelle gesucht?


IsNull - Di 19.10.10 20:49

Nein das hab ich noch nicht, werde ich morgen mal nachholen.

Ich hatte heute gerade an einem anderen Programm zu tun, - ich vermag mich zu täuschen, aber ich bin mir ziemlich sicher, in einem ganz anderen Zusammenhang ebenfalls eine Exception in diesem blöden {System.Collections.ListDictionaryInternal} gehabt zu haben. Lustiger weise war es aber in Wahrheit ein Fehler von mir, der dort warum auch immer intern knallte.

Ich musste letzte Woche VS 2008 SP1 über mein VS 2010 installieren*, evtl hat das ein paar dlls zerschossen/ mit alten Versionen ersetzt.

*Die Begründung in dem Unterfangen lag in der Tatsache, dass die M$ Leute entschieden hatten, dass VS 2010 keinen Support für meinen SQL 2000 Server hat. o.0


Ralf Jansen - Di 19.10.10 21:05

Zitat:
Ich musste letzte Woche VS 2008 SP1 über mein VS 2010 installieren*, evtl hat das ein paar dlls zerschossen/ mit alten Versionen ersetzt.


Nicht unwahrscheinlich. Die letzte Methode auf deinem Stack 'NotifyMASSClient' gibts nur im 4er Framework. Deine Projekte die unter VS2008 maximal mal als 3.5 bauen referenzieren also die 4er Version vom zumindest Winforms. Vielleicht solltest du erstmal die Referenzen aufs Framework in deinem Projekt kontrollieren und gegebenenfalls ~zurückdrehen~.


IsNull - Do 21.10.10 10:49

Hm, ich hab nun die Klasse DataGridViewCheckBoxCell gedebugt



C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
        private void NotifyMASSClient(Point position)
        { 
            Debug.Assert((position.X >= 0) && (position.X < this.DataGridView.Columns.Count));
            Debug.Assert((position.Y >= 0) && (position.Y < this.DataGridView.Rows.Count));


            int visibleRowIndex = this.DataGridView.Rows.GetRowCount(DataGridViewElementStates.Visible, 0, position.Y); //<--- Hier Exception!
            int visibleColumnIndex = this.DataGridView.Columns.ColumnIndexToActualDisplayIndex(position.X, DataGridViewElementStates.Visible);
 
            int topHeaderRowIncrement = this.DataGridView.ColumnHeadersVisible ? 1 : 0
            int rowHeaderIncrement = this.DataGridView.RowHeadersVisible ? 1 : 0;
 
            int objectID = visibleRowIndex + topHeaderRowIncrement  // + 1 because the top header row acc obj is at index 0
                                           + 1;                     // + 1 because objectID's need to be positive and non-zero

            int childID = visibleColumnIndex + rowHeaderIncrement;  // + 1 because the column header cell is at index 0 in top header row acc obj 
                                                                    //     same thing for the row header cell in the data grid view row acc obj
 
            (this.DataGridView.AccessibilityObject as Control.ControlAccessibleObject).NotifyClients(AccessibleEvents.StateChange, objectID, childID); 
        }


Exception:
this.DataGridView
'this.DataGridView' threw an exception of type 'System.NullReferenceException'
base {System.SystemException}: {"The pointer for this method was null."}



DataGridViewCheckBoxCell will also auf sein Parent, das DGV zugreifen, und das wirft obige Excpetion.


Und was hilft mir das jetzt? Ich kann ja nicht das Framework fixen :/


@Ralf Jansen:
Also ich bin hier unter VS2010 .net 4.0, ich glaube du hast mich falsch verstanden. ;)