How to avoid a "object reference not set to an instance of an object" exception in XAML code at design time?

46,718

Solution 1

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.

You can 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 and screenshots.

Solution 2

Whatever is happening in your constructor is throwing an exception during design time. I had same problem - I just put a try catch around the problematic code - in my case I was calling ServiceLocator.Current as I am using an IoC container. But there is no container during design time. So I wrapped in a try catch to suppress the error and it worked. Not the best solution... but its a solution.

Solution 3

I tend to use the LicenseManager class in System.ComponentModel to avoid my ViewModels throwing nasty errors at designtime. For example:

public MyViewModel()
{
  if (LicenseManager.UsageMode == LicenseUsageMode.Runtime)
  {
    // Do runtime stuff
  }
}

Solution 4

Tweaking @BobHorn's example, I got this to work for me:

public class ViewModel
{
    public ViewModel()
    {
        if (!IsInDesignMode)
        {
            //Constructor code here...
        }
    }
    public bool IsInDesignMode
    {
        get
        {
            var prop = DesignerProperties.IsInDesignModeProperty;
            return (bool)DependencyPropertyDescriptor
                .FromProperty(prop, typeof(FrameworkElement))
                .Metadata.DefaultValue;
        }
    }
}

Though using his exact suggestion for the constructor

public Main()
{
    if (IsInDesignMode) { return; }
    //Constructor code here...
}

Did also work for me, I just prefer not short-circuiting my methods with extra return statements. I would have up-voted his answer, don't have the rep yet.

Solution 5

I had the similar problem. You just need to go to Tools> Options> XAML Designer and enable the option

"Run project code in XAML designer".

Finally restart Visual Studio. I hope this will help.

Share:
46,718
Kimbo
Author by

Kimbo

Updated on July 29, 2022

Comments

  • Kimbo
    Kimbo almost 2 years

    I have a problem with a wpf usercontrol which is of my own devising. The problem is that I get a object reference not set to an instance of an object exception in XAML code at design time, when I implement the usercontrol in my program.

    The designer showed me the following information:

    at
    Microsoft.Expression.Platform.InstanceBuilders.InstanceBuilderOperations.InstantiateType(Type
    type, Boolean supportInternal)    at
    Microsoft.Expression.Platform.InstanceBuilders.ClrObjectInstanceBuilder.InstantiateTargetType(IInstanceBuilderContext
    context, ViewNode viewNode)    at
    Microsoft.Expression.Platform.InstanceBuilders.ClrObjectInstanceBuilder.Instantiate(IInstanceBuilderContext
    context, ViewNode viewNode)    at
    Microsoft.Expression.WpfPlatform.InstanceBuilders.FrameworkElementInstanceBuilder.Instantiate(IInstanceBuilderContext
    context, ViewNode viewNode)    at
    Microsoft.Expression.WpfPlatform.InstanceBuilders.UserControlInstanceBuilder.Instantiate(IInstanceBuilderContext
    context, ViewNode viewNode)    at
    Microsoft.Expression.Platform.InstanceBuilders.ViewNodeManager.CreateInstance(IInstanceBuilder
    builder, ViewNode viewNode)
    

    but I think these messages are not really helpful...

    How can I fix or suppress this exception?

  • jv42
    jv42 over 8 years
    Which version of Visual Studio are you using? I can't find the "XAML Designer" section in Visual Studio 2013.
  • Alan Wayne
    Alan Wayne about 6 years
    If the designer on the current xaml does not load, the "Disable project code" button will not be available. Load a good xaml to make the button available. The setting will now be applied to the xaml which refuses to load.
  • Dark Knight
    Dark Knight almost 6 years
    Sorry it's a bit late. Anyways I was using VS 2015.
  • jv42
    jv42 almost 6 years
    Ah ah, that's late, right :) I've indeed found this option in more recent versions of VS.
  • N_tro_P
    N_tro_P almost 5 years
    This answer makes 0 sense. The issue is in a XAML and there is no way to "just put a try catch" around that. If a person knew what the "problematic code" was there wouldn't be a problem.
  • Shumii
    Shumii almost 5 years
    @N_tro_P There is a .cs file behind the xaml which has a constructor.
  • N_tro_P
    N_tro_P almost 5 years
    Do you think that wrapping a try catch around InitializeComponent() is going to catch the problematic code? Also... Some XAML does NOT have a constructor, such as a resource dictionary. This in particular is my case and how I ended up here.
  • N_tro_P
    N_tro_P almost 5 years
    I do not think .Net design time errors care about your beliefs. Again, there isn't even necessarly a code behind. Perhaps in the posters case as in the details he states "user control", but the question is about how to handle a design time error in XAML which could also be a RD. There is no constructor in such a case.
  • Shumii
    Shumii almost 5 years
    Well may be it does not apply in your case. I think if you share your specifics and how you solved it, that would be more productive than rhetoric.
  • N_tro_P
    N_tro_P almost 5 years
    I haven't solved it... If I did, I would post that answer.
  • Majid khalili
    Majid khalili almost 5 years
    if you don't see buttons, try to comment the propblamitc lines and then you will get it
  • Vidar
    Vidar about 4 years
    I had some code in my constructor that caused this manifestation to occur - it was bugging me (so to speak) glad you helped me out here.
  • Kajetan Jauk
    Kajetan Jauk over 3 years
    So an answer every two years? :b I am using VS 2019 and can't find that option in the XAML Designer menu..
  • windowsill
    windowsill over 2 years
    In my VS that button gives me the option to "display all controls", or "only display platform controls". Choosing the latter causes the xaml to render in the designer and removes the object reference not set to an instance of an object error.