Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What caused the NullReferenceException in WpfXamlLoader.TransformNodes ()?

I am trying to track down a problem that only happens in release mode and is most likely caused by the invalid obfuscation of some property. I know it happens when initializing a specific control but this control is huge. I have spent a day going through all the XAML and Bindings and still can't see what is causing this exception.

Is there any way to get more information. To know what caused this exception?

Exception : System.NullReferenceException
Message   : Object reference not set to an instance of an object.
Source    : PresentationFramework
Help      : 
Stack     :
   at System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack`1 stack, IStyleConnector styleConnector)
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
   at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   at MyClass.InitializeComponent()
like image 809
Patrick Klug Avatar asked Feb 03 '11 03:02

Patrick Klug


People also ask

How do I fix NullReferenceException in C#?

You can eliminate the exception by declaring the number of elements in the array before initializing it, as the following example does. For more information on declaring and initializing arrays, see Arrays and Arrays. You get a null return value from a method, and then call a method on the returned type.

What does system NullReferenceException mean?

The runtime throwing a NullReferenceException always means the same thing: you are trying to use a reference, and the reference is not initialized (or it was once initialized, but is no longer initialized). This means the reference is null , and you cannot access members (such as methods) through a null reference.

How do I fix this error system NullReferenceException Object reference not set to an instance of an object?

The best way to avoid the "NullReferenceException: Object reference not set to an instance of an object” error is to check the values of all variables while coding. You can also use a simple if-else statement to check for null values, such as if (numbers!= null) to avoid this exception.

Can NullReferenceException be generally caught?

Handling the error Being a plain old C# exception, NullReferenceException can be caught using a try/catch : try { string s = null; s.


1 Answers

I don't know a way to get a more detailed exception message, but it might at least be useful to other people to know possible causes. I've just tracked a NullReferenceException in WpfXamlLoader.TransformNodes down to a DependencyProperty which was registered with DependencyProperty.Register(string, Type, Type). Changing

public static readonly DependencyProperty FooProperty = DependencyProperty.Register(
        nameof(Foo), typeof(object), typeof(Bar));

to

public static readonly DependencyProperty FooProperty = DependencyProperty.Register(
        nameof(Foo), typeof(object), typeof(Bar), new FrameworkPropertyMetadata(null));

fixed the problem.

like image 163
Peter Taylor Avatar answered Nov 15 '22 09:11

Peter Taylor