Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF, 'Object reference not set to an instance of an object' in Designer

I'm getting an "Object reference not set to an instance of an object" error when I try to reload the Designer for my XAML UserControl. Visual Studio highlights the following line as being the problem:

<local:TemplateDetail Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="3"     Width="600" TemplateData="{Binding ElementName=cbo_templates,                                Path=SelectedItem.Data, Mode=OneWay}"/> 

TemplateDetail is another UserControl. When I view TemplateDetail, its Designer view loads just fine, so I don't think there's a problem there. There is a ComboBox in my XAML named cbo_templates that contains instances of my Template class, which has a Data property (hence SelectedItem.Data). However, if I remove .Data from the Path in the above XAML, I still get the "Object reference" error, so I don't think the problem is that I'm trying to access the Path property on null. Here's my ComboBox XAML just in case:

<ComboBox ItemsSource="{Binding Path=List}" Grid.Row="1" Grid.Column="3"           VerticalAlignment="Center" x:Name="cbo_templates" Width="250"           HorizontalAlignment="Left" DisplayMemberPath="Name"           SelectedValuePath="Name" SelectedIndex="0"/> 

Getting this error is a real problem because the Design view won't load, so I can't see what my UserControl looks like without running the app. Any idea what could be wrong? It builds fine and I don't see any binding problems in the Build Output.

Edit: here is the constructor code for both UserControls:

Constructor of UserControl with "Object reference" error:

InitializeComponent(); grd_templateList.DataContext = this; // refers to containing <Grid> in XAML 

Constructor of UserControl I'm trying to embed, the one whose Design view loads okay:

InitializeComponent(); grd_templateDetail.DataContext = this; // refers to containing <Grid> in XAML 

Edit: I tried putting an if (null != grd_templateList) check in the constructors before setting their DataContext properties, but that didn't help--still getting the "Object reference" error when reloading the Designer.

Edit: the List property that the ComboBox uses is a DependencyProperty. I have a default value set in the Register method:

public static readonly DependencyProperty ListProperty =     DependencyProperty.Register(         "List",         typeof(List<Template>),         typeof(TemplateList),         new PropertyMetadata(             new List<Template> { _defaultTemplate }         )     ); 

Even if I try to initialize List in the constructor for my UserControl, I still get the error when reloading the Designer. I don't think the problem is that List is null or SelectedItem.Data is a bad path.

Edit: okay, even just having this causes my Designer to not load, giving the "Object reference" error:

<local:TemplateDetail Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="3"                       TemplateData="{Binding}"/> 

There is something it dislikes about the TemplateData property being bound, apparently.

Edit: to add to the mystery, I can view the Design view of my overall/main Window, which includes the UserControl whose Design view gives me the "Object reference" error. O_o

like image 484
Sarah Vessels Avatar asked Aug 20 '10 17:08

Sarah Vessels


People also ask

How do I fix object reference not set to an instance?

To fix "Object reference not set to an instance of an object," you should try running Microsoft Visual Studio as an administrator. You can also try resetting the user data associated with your account or updating Microsoft Visual Studio to the latest version.

What is object reference not set to an instance of an object in C#?

So, this error description says that an object that is being called to get or set its value has no reference. This means that you are trying to access an object that was not instantiated.

How do I fix object reference not set to an instance of an object in VB net?

"instance of object" Means that the object is blank (or in VB speak, "Nothing"). When you are dealing with object variables, you have to create an instance of that object before referencing it. "not set to an " means that you tried to access an object, but there was nothing inside of it for the computer to access.


2 Answers

What Alex says is the way to go. But I think its a little confusing to understand what he is saying.

Assuming you have your project open in Visual Studio, open another Visual Studio instance and select Debug->Attach To Process. In the dialog which opens select

  • XDesProc.exe (which is the XAML UI Designer) for VS2012 and newer or
  • devenv.exe for older VS versions.

Then do "Reload Designer" for the user control and see the output in the second VS instance to check what exactly is the error.

like image 51
NVM Avatar answered Oct 14 '22 14:10

NVM


If you have 'Object reference not set to an instance of an object' in XAML, but your application compiles and runs fine, you will usually find out that its cause is something in a constructor that can't be resolved at design time.

While you can find out the root of the problem with the help of other answers to this question, sometimes that is something you can't simply fix, you need it in your code exactly as you have it, but you don't want to see this error.

In this case, just click the "Disable project code" button located on the bottom of your designer view and Visual Studio designer will stop trying to construct an instance to provide design time data view.

See here for detailed information.

enter image description here

like image 37
Lirrik Avatar answered Oct 14 '22 15:10

Lirrik