6

This code supposed to mark my Section as optional.

_layout.cshtml 
@RenderSection("ViewStyles",false)

or

@RenderSection("ViewStyles",required:false)

I tried both.

Yet, it throws an exception

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

I tried adding an empty section to my views that don't need to use the ViewStyles section

@section ViewStyles {  }

but I still get the same exception.

Is there another workaround?

1
  • every time I have seen that error it is because of using a list that wasn't initialized. Make sure you have list = new list on all of your lists before you populate them Commented Nov 19, 2014 at 17:27

3 Answers 3

7

@Chris-Pratt led me in the right direction with his detailed answer. I wanted to share my code to help out:

My issue was @RenderSection("Header", false) coming back “Object reference not set to an instance of an object”. Stepping throw the code, it would not step pass and break:

header section

Finally, I realized it was the next line of code that breaking:

<body class="@TempData[AppConstants.GlobalClass].ToString() 
         loggedin-@(User.Identity.Name.ToString().ToLower())">

@TempData was null

Sign up to request clarification or add additional context in comments.

Comments

3

This has nothing to do with your section. I'm not sure where you got the idea that the section being empty is generating this error, but that is categorically not what's happening. Object reference not set to an instance of an object is a runtime error generated when you attempt to reference a property off of a variable that evaluates to null. There's some piece of code somewhere that is referencing a property off a variable (again, not talking about sections here) when that variable itself resolves to null at runtime.

For example, lets say you do something like:

Foo foo = db.Foos.Find(id);

The variable foo is defined as a Foo, so you can reference any property off of it that Foo has. If your Foo class had a property named Bar. Then you might then try to get the value of this property somewhere in your code via:

foo.Bar

That will compile just fine. However, if no Foo with the id is found, then the actual value of foo is null, and null does not have a property named Bar, which can only be determined at runtime. That is what the error is telling you is happening: somewhere in your code, you're calling a property of some variable without checking for a null value of the variable first. In the example above, you would typically do something like:

Foo foo = db.Foos.Find(id);
if (foo != null)
{
    bar = foo.Bar;
}

You can also employ a ternary to provide some sort of fallback:

bar = foo != null ? foo.Bar : "Baz";

That way, bar will either hold the value of foo.Bar or if foo is null, the string "Baz".

2 Comments

OMG... No, I'm not saying the section resolves to null; I'm saying you're not doing proper null checking in some other area of your application. The error has absolutely nothing whatsoever at all to do with the section.
you're absolutely correct about "some code returning" null and its properties are being used without proper checks. At the end, the problem was in the _contentLayout. It expected a specific model to be passed to it from the controller. I am not sure why the debugger chose to highlight the RenderSection() among all other helper methods. I apologize for my negative comment! Your answer did send me on the right path to find the solution. Thank you!
1

I had the @RenderSection("ViewStyles",required:false) but was still getting error due to a missing app setting which is being used in my layout page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.