Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for the source of a NullReferenceException in a ShowDialog() method

Tags:

c#

wpf

This is in WPF (C#).

I am trying to find what object is throwing a NullReferenceException when I call the ShowDialog() method in a Window object. The code is similar to this:

MyWindow myWindow = new MyWindow();
//Some properties of myWindows are set here
try
{
    myWindow.ShowDialog();
}
catch (Exception) //In here I catch a NullReferenceException
{

}

Is there a way to find what's causing the exception? I placed a Breakpoint in the very beginning of the 'Window_Loaded' Event Handler in the MyWindow class but it just doesn't trigger.

like image 841
Arturo Gurrola Avatar asked Dec 16 '22 02:12

Arturo Gurrola


1 Answers

Go to Debugging > Exceptions and tick the "Thrown" checkbox for the Common Language Runtime Exceptions (or more specifically if you wish, the NullReferenceException deeper down in the treeview)

Then run, and the debugger will catch the exception on the line where it is thrown.

like image 153
Jason Williams Avatar answered May 19 '23 21:05

Jason Williams