NullReferenceException

  • Thread starter Thread starter Mats-Erik Grundh
  • Start date Start date
M

Mats-Erik Grundh

NullReferenceException?

I receive this exception when i run my application:

******** Begin Exception ********
"System.NullReferenceException: Object reference not set
to an instance of an object.
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.Run(Form mainForm)
at CO.Win.MainModule.Main() in
C:\CODev\Utveckling\CO\CO.Win\Main.vb:line 128"
******** End Exception ********

It's impossible to find out where in my code its been
fired. I've got try-catch statements everywhere but the
exception appears only in my main module:

try
..
..
Application.Run(startForm)
Catch ex As System.NullReferenceException
End try

There is a datagrid on my form.

Thanks in advanced!
 
Hi,

Is your data grid somehow customized? I have seen similar errors when custom
column styles were used and these column styles contained bugs.
 
Yes i customize my datagrid with custom column styles. Are
there some ways to debug these exceptions? Do Microsoft
have guidlines/recommendations how to design custom column
styles?

Thanks!

-----Original Message-----
Hi,

Is your data grid somehow customized? I have seen similar errors when custom
column styles were used and these column styles contained bugs.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Unit Testing and Integration Environment
http://x-unity.miik.com.ua
Deliver reliable .NET software

Mats-Erik Grundh said:
NullReferenceException?

I receive this exception when i run my application:

******** Begin Exception ********
"System.NullReferenceException: Object reference not set
to an instance of an object.
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.Run(Form mainForm)
at CO.Win.MainModule.Main() in
C:\CODev\Utveckling\CO\CO.Win\Main.vb:line 128"
******** End Exception ********

It's impossible to find out where in my code its been
fired. I've got try-catch statements everywhere but the
exception appears only in my main module:

try
.
.
Application.Run(startForm)
Catch ex As System.NullReferenceException
End try

There is a datagrid on my form.

Thanks in advanced!

.
 
I've tried "Stop when an exeption occurs" but it doesnt
help.

The nullreferenceexception occurs before i even started
editing och clicking the datagrid.

The consequences of this exception is that this mission
ciritical application crasch.

What to do?

Thanks in advance!

-----Original Message-----
I have seen an article in MSDN outlining creation of a custom column style,
but as far as I remember it just covers the basics.

As for the debugging, enable "Stop when an exception occurs" mode in the
debugger to see where exactly the exception is raised. This might help and
might not help as well if the exception occurs within the DataGrid code.

Besides of this, you should also ensure that:

- If you override the TextBoxColumn's Commit method, DO ENSURE that you call
the base class implementation. Otherwise you'll have odd exceptions raised
when a cell is being edited.

- Ensure that you do not pass unexpected values to base class
implementations

Generally speaking, such an exception indicates that your code does somthing
that breaks the grid's internal state - and this is caused mostly by lack of
calling the base class implementation. Extreme care needs to be taken with
the Edit, Abort, Commit and ConcedeFocus methods as they all participate in
a complex sequence of the column lifecycle within the grid.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Unit Testing and Integration Environment
http://x-unity.miik.com.ua
Deliver reliable .NET software

Mats-Erik Grundh said:
Yes i customize my datagrid with custom column styles. Are
there some ways to debug these exceptions? Do Microsoft
have guidlines/recommendations how to design custom column
styles?

Thanks!

.
 
1. Have you tried the rest of my suggestions?
2. Try to look at the stack trace after stopping program execution upon
NullReferenceException. This might help you figure out the approximate error
location - compare the method names in the stack trace with those you have
overridden, for example.
 
I've tried the rest bu with no results.

The stacktrace is:

******** Begin Exception ********
"System.NullReferenceException: Object reference not set
to an instance of an object.
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.Run(Form mainForm)
at CO.Win.MainModule.Main() in
C:\CODev\Utveckling\CO\CO.Win\Main.vb:line 128"
******** End Exception ********

-----Original Message-----
1. Have you tried the rest of my suggestions?
2. Try to look at the stack trace after stopping program execution upon
NullReferenceException. This might help you figure out the approximate error
location - compare the method names in the stack trace with those you have
overridden, for example.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Mats-Erik Grundh said:
I've tried "Stop when an exeption occurs" but it doesnt
help.

The nullreferenceexception occurs before i even started
editing och clicking the datagrid.

The consequences of this exception is that this mission
ciritical application crasch.

What to do?

Thanks in advance!

.
 
The stack trace eventually points to a Windows message handler. Do you
implement any custom Windows message handlers (say, by overriding WndProc or
PreProcessMessage methods)?

If you will be able to see the "msg" structure contents, you'll find out the
exact message that causes the data grid to fail.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Mats-Erik Grundh said:
I've tried the rest bu with no results.

The stacktrace is:

******** Begin Exception ********
"System.NullReferenceException: Object reference not set
to an instance of an object.
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.Run(Form mainForm)
at CO.Win.MainModule.Main() in
C:\CODev\Utveckling\CO\CO.Win\Main.vb:line 128"
******** End Exception ********

-----Original Message-----
1. Have you tried the rest of my suggestions?
2. Try to look at the stack trace after stopping program execution upon
NullReferenceException. This might help you figure out the approximate error
location - compare the method names in the stack trace with those you have
overridden, for example.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Mats-Erik Grundh said:
I've tried "Stop when an exeption occurs" but it doesnt
help.

The nullreferenceexception occurs before i even started
editing och clicking the datagrid.

The consequences of this exception is that this mission
ciritical application crasch.

What to do?

Thanks in advance!

.
 
Is the "msg" structure data saved in the
nullreferenceexception exception?
-----Original Message-----
The stack trace eventually points to a Windows message handler. Do you
implement any custom Windows message handlers (say, by overriding WndProc or
PreProcessMessage methods)?

If you will be able to see the "msg" structure contents, you'll find out the
exact message that causes the data grid to fail.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Mats-Erik Grundh said:
I've tried the rest bu with no results.

The stacktrace is:

******** Begin Exception ********
"System.NullReferenceException: Object reference not set
to an instance of an object.
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.Run(Form mainForm)
at CO.Win.MainModule.Main() in
C:\CODev\Utveckling\CO\CO.Win\Main.vb:line 128"
******** End Exception ********

-----Original Message-----
1. Have you tried the rest of my suggestions?
2. Try to look at the stack trace after stopping
program
execution upon
NullReferenceException. This might help you figure out the approximate error
location - compare the method names in the stack trace with those you have
overridden, for example.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

I've tried "Stop when an exeption occurs" but it doesnt
help.

The nullreferenceexception occurs before i even started
editing och clicking the datagrid.

The consequences of this exception is that this mission
ciritical application crasch.

What to do?

Thanks in advance!

.

.
 
No, it is not. I just hope you would be able to inspect this structure when
you double click the corresponding row in the Stack Trace. That's how the
debugger works - it can inspect variables only from current context, and
contexts are switched when you double-click in the Stack Trace window.
Unfortunately, I can't be 100% sure of this - I don't do such an advanced
debugging frequently.

Anyway, I think the first things to be throughly checked are:

- Custom painting
- Custom keyboard handling
- Custom mouse handling

As you have said you experience this error even when you don't edit or
navigate the grid, custom painting would be the first candidate, I think.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Mats-Erik Grundh said:
Is the "msg" structure data saved in the
nullreferenceexception exception?
-----Original Message-----
The stack trace eventually points to a Windows message handler. Do you
implement any custom Windows message handlers (say, by overriding WndProc or
PreProcessMessage methods)?

If you will be able to see the "msg" structure contents, you'll find out the
exact message that causes the data grid to fail.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE
 
The only row left in the stacktrace window is currentrow.

There are no owner drawn/overide paint, keaybord or
mousehandling in the form.

1. clickes an ordinary Button
2. in buttons click event i call a sub that catch data
from server.
3. i create a ordinary new datagrid with custom
columnstyles.
4. assign datatable to datagrid.


-----Original Message-----
No, it is not. I just hope you would be able to inspect this structure when
you double click the corresponding row in the Stack Trace. That's how the
debugger works - it can inspect variables only from current context, and
contexts are switched when you double-click in the Stack Trace window.
Unfortunately, I can't be 100% sure of this - I don't do such an advanced
debugging frequently.

Anyway, I think the first things to be throughly checked are:

- Custom painting
- Custom keyboard handling
- Custom mouse handling

As you have said you experience this error even when you don't edit or
navigate the grid, custom painting would be the first candidate, I think.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Mats-Erik Grundh said:
Is the "msg" structure data saved in the
nullreferenceexception exception?
-----Original Message-----
The stack trace eventually points to a Windows message handler. Do you
implement any custom Windows message handlers (say, by overriding WndProc or
PreProcessMessage methods)?

If you will be able to see the "msg" structure
contents,
you'll find out the
exact message that causes the data grid to fail.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

.
 
3. i create a ordinary new datagrid with custom
columnstyles.

What exactly these column styles customize? I am pretty sure the problem is
one of these column styles - I have had similar unpredictable exceptions
myself and they were definitely related to bugs in the custom column styles.

More on that - do you follow the correct order of table style/column style
creation given in MSDN?
4. assign datatable to datagrid.

How to you do that? I'd suggest sticking to the SetDataBinding method.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Mats-Erik Grundh said:
The only row left in the stacktrace window is currentrow.

There are no owner drawn/overide paint, keaybord or
mousehandling in the form.

1. clickes an ordinary Button
2. in buttons click event i call a sub that catch data
from server.
3. i create a ordinary new datagrid with custom
columnstyles.
4. assign datatable to datagrid.


-----Original Message-----
No, it is not. I just hope you would be able to inspect this structure when
you double click the corresponding row in the Stack Trace. That's how the
debugger works - it can inspect variables only from current context, and
contexts are switched when you double-click in the Stack Trace window.
Unfortunately, I can't be 100% sure of this - I don't do such an advanced
debugging frequently.

Anyway, I think the first things to be throughly checked are:

- Custom painting
- Custom keyboard handling
- Custom mouse handling

As you have said you experience this error even when you don't edit or
navigate the grid, custom painting would be the first candidate, I think.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Mats-Erik Grundh said:
Is the "msg" structure data saved in the
nullreferenceexception exception?

-----Original Message-----
The stack trace eventually points to a Windows message
handler. Do you
implement any custom Windows message handlers (say, by
overriding WndProc or
PreProcessMessage methods)?

If you will be able to see the "msg" structure contents,
you'll find out the
exact message that causes the data grid to fail.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

.
 
Back
Top