Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.NullReference Exception occurred in App_Web_XXX.dll - Error in Partial View

I'm getting this error, with a random string in the dll name, when landing on one of my MVC pages.

A first chance exception of type System.NullReferenceException occurred in App_Web_cjmhrrqn.dll. Additional information: Object reference not set to an instance of an object.

on another page, I get similar when hitting the first line of code on the page, not matter what it is:

A first chance exception of type System.NullReferenceException occurred in App_Web_register.cshtml.5f83eb8c.gq-8jyy8.dll

However, this is not a problem with the code. I've removed the lines it fails at and it continues to fail at the next line of code. I can even put several lines of new code and it will break at the last one.

It happens in a couple of places throughout the application.

It also happens on my Azure deployment.

The error does not help in any way to find the problem.

My suspicion is some kind of DLL issue but I have cleared out my bin, obj, and ASP.NET temporary folders.

Has anyone seen this issue, or have any ideas? Are there any other temporary folders I need to clear? Or any other way to mitigate this issue? How come it is still on my live site too?

like image 285
stuartdotnet Avatar asked Nov 02 '22 00:11

stuartdotnet


1 Answers

OK so guess what...it WAS a problem with the code.

The problem was that there was an issue in the Partial view. I was trying to use Model.SomethingNull without checking it.

The lesson to be learnt here though (apart from check for null!) is that when there is an issue like this in a partial view which you are calling using Html.Partial, Visual Studio won't step through the lines of the Partial view. It will just fail at the start of the partial and won't tell you that there is a problem somewhere in it.

This also explains why the problem was present in all environments.

like image 183
stuartdotnet Avatar answered Nov 15 '22 06:11

stuartdotnet