Home:ALL Converter>Why can there be a NullReferenceException?

Why can there be a NullReferenceException?

Ask Time:2014-01-05T06:29:09         Author:BennoDual

Json Formatter

I have the following code:

try {
    using (var stream = new MemoryStream()) {
        var ms = stream;
        if (control is DockLayoutManager) {
            if (control.Dispatcher == null || control.Dispatcher.CheckAccess()) {
                ((DockLayoutManager)control).SaveLayoutToStream(ms);
            } 
        }
    }
} catch (Exception e) {
    log.Error(string.Format("Cannot GetLayout ({0}).", typeName), e);
}

From time to time, I get a NullReferenceException on the line

((DockLayoutManager)control).SaveLayoutToStream(ms);

I have no idea, why there can be a NullReferenceException on this line.

I hope, someone can help me.

Author:BennoDual,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/20927540/why-can-there-be-a-nullreferenceexception
NotMe :

The problem is inside .SaveLayoutToStream(ms)\n\nThis has happened several times before:\n\nhttps://www.devexpress.com/Support/Center/Question/Details/B190607\nhttp://www.devexpress.com/Support/Center/Question/Details/B221485\nhttps://www.devexpress.com/Support/Center/Question/Details/Q445171\n\nIf you have the source code from them, then I'd link those into the solution, rebuild and trace through when it failed. Also I'd make sure my source/binaries were completely up to date. \n\nIf you don't, then debugging this is going to be extra hard. DevExpress has indicated they have no desire to put the code in place to throw a solid enough error for you to be able to pinpoint the exact cause.. Also, the reason why the compiler is throwing on that line is simply that it doesn't have any further source code lines to point you to and it's happening inside that method. In which case, contact DevExpress to ask them what's up. \n\nSeems to be a serialization issue. The B221485 issue number appears to indicate that having a control with a property of type DefaultBoolean is being set to -1 and subsequently blowing up. Support said to locate any of those properties where you are setting such a property to true or false as being indicators of what to fix. seems odd.",
2014-01-04T22:41:03
usr :

If this code behaves like it obviously appears to do, this line cannot throw a nullref. Something else is going on. Put a breakpoint there and observe runtime behavior.\n\nIdeas:\n\n\nSomething in control.Dispatcher.CheckAccess could set control to null. That would be horrible code indeed.\nIt is a threading bug (a race).\nOutdated source file. Rebuild the solution.\nSomething inside of SaveLayoutToStream threw and you misinterpreted the location exception. Look at the stack trace to find out. Set the debugger to break on exceptions (Ctrl-Alt-E).\n",
2014-01-04T22:39:15
yy