Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mystery System.Object.GetType() NullReferenceException

Tags:

c#

casting

We experienced a crash in our program that we are now unable to reproduce. I am trying to put in some code to prevent it from happening again but I am confused over the stack trace.

System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Object.GetType()
   at Project.ViewModel.MainVM.<CreateCommands>b__8(Object a)
   at System.Windows.Controls.Button.OnClick()

-- I have cut down the stack trace as it just goes into a load of system code which is just to do with the button being clicked. --

I have managed to deduce that it is pointing to my anonymous delegate on line 8 of my CreateCommands method.

        this.sectionCommand = new DelegateCommand(a =>
        {
            this.OnSectionParameterChanged((Sections)a);
        }, p => this.IsSectionCommandExecutable);

I have seen a similar post on here but the OP was calling GetType explicitly. I am assuming that the cast calls get type, but without being able to reproduce the issue I cannot see what is null.

So my question is: For this stack trace to cause a null reference, is the 'a' variable the null object? (so I would write something like)

            if (a != null)
            {
                this.OnSectionParameterChanged((Sections)a);
            } 

or is the cast from 'a' to 'sections' causing a null object? (so I should write something like)

            if (a is Sections)
            {
                this.OnSectionParameterChanged((Sections)a);
            } 

As requested here is OnSectionParameterChanged

    private void OnSectionParameterChanged(Sections parameter)
    {
        this.SelectedSection = parameter;

        this.RaisePropertyChanged(() => this.SelectedSection);

        this.LoadSettingsPanel();
    }

further to that it calls a LoadSettingsPanel

    private void LoadSettingsPanel()
    {
        if (sectionVMs == null)
            return;

        // Get section
        SectionViewModel = sectionVMs.SingleOrDefault(s.SectionName == SelectedSection);

        this.IsSelectedSectionEnabled = this.Config.GetIsSectionEnabled(this.SelectedSection);

        this.RaisePropertyChanged(() => this.IsSelectedSectionEnabled);

        // Set advanced
        AdvancedViewModel = this.SectionViewModel;

        if (AdvancedViewModel != null)
            HasAdvanced = AdvancedViewModel.HasAdvanced;
    }
like image 463
Keithin8a Avatar asked Jul 06 '15 13:07

Keithin8a


People also ask

How do I fix 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.

Why am I getting a NullReferenceException?

This error is caused when an object is trying to be used by a script but does not refer to an instance of an object. To fix this example we can acquire a reference to an instance of the script using GameObject.

What does system NullReferenceException mean?

A NullReferenceException exception is thrown when you try to access a member on a type whose value is null . A NullReferenceException exception typically reflects developer error and is thrown in the following scenarios: You've forgotten to instantiate a reference type.


1 Answers

The problem that I had described was actually not the real problem. I read on a different site that the < CreateCommands >b__8 part of the stack trace meant that the issue was on line 8 of the CreateCommands method. This lined up exactly with an anonymous delegate and I could see how it matched the behaviour in the bug report.

I actually found the solution to my problem by using IL Dasm (which can be found in

\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin

and opened the EXE which was run and found what the .net thought b__8 actually was. This turned out to be another anonymous delegate which was explicitly calling .GetType() so the problem was actually really easy once I found out what b__8 actually meant.

like image 130
Keithin8a Avatar answered Oct 06 '22 00:10

Keithin8a