I am using MVC with Razor views. In this particular view, I am passing a single instance of the class Bed
. Bed
has a property string Infection
. Now in this instance, I have a boolean HasInfection
defined in the view that I am using elsewhere to change what is displayed. This was originally declared as
var HasInfection = (Model.Infection.Trim() != "";
and worked as expected. However, there is now a use case where Bed
may be null. Here is that first block of code:
@{
ViewBag.Title = "Edit";
var HasInfection = false;
if (Model != null)
{
HasInfection = Model.Infection.Trim() != "";
} // I get a NRE on this line whenever Model is null
}
I have even tried the convoluted nested if-else solution, and I still get an NRE on the closing brace of if
.
if (Model.Infection == null)
{
HasInfection = false;
}
else
{
if (Model.Infection != "")
{
HasInfection = true;
}
else
{
HasInfection = false;
}
}
I've tried every combination of &/&&/|/|| I can think of with no success. If Model
is null
or Model.Infection == ""
, HasInfection
should be false
.
What am I doing wrong?
EDIT
After attempting var HasInfection = Model != null && !string.IsNullOrWhiteSpace(Model.Infection);
(since Infection
could be " "), I still get a NullReferenceException. Is it possible the issue is in the Controller even if the exception is in the View?
public ActionResult EditReservation(int Facility, string Room, string Bed)
{
var BedModel = New Bed();
List<Bed> _b = BedModel.GetBed(Facility, Room, Bed);
Bed result = _b.Where(bed => bed.BedStatus == "R" || bed.BedStatus == "A").FirstOrDefault();
return View("Edit", result);
}
You get a null return value from a method, and then call a method on the returned type. This sometimes is the result of a documentation error; the documentation fails to note that a method call can return null . In other cases, your code erroneously assumes that the method will always return a non-null value.
The Null Reference Exception is not a major error, but one of the common ones and one of the basic and simple way to avoid the Null Reference Exception is to check the variable or property before moving ahead and accessing it. And a very basic way to do this is to check the variable within an if statement.
I had the same problem.
When looking to the second comment in this question I found out my exception was actually 35 lines further down the road (outside the brackets) than the closing bracket where the NullReferenceException pointed to.
For ex:
if(Model.Infection != null)
{
<p>Some Html</p>
} //given NullReferenceException location
<p>Model.Infection</p> //Actual cause of NullReferenceException since
//here Model.Infection can be null
You should be able to do something like this:
var HasInfection = Model != null && !String.IsNullOrWhitespace(Model.Infection)
I can't think I've ever passed a null model to a Razor view, but I can't see why this wouldn't work.
As an aside, you could consider having a 'NullBed' object you could pass, which behaved in whatever way you deemed right for a 'missing' bed object - then you wouldn't need to be checking for nulls all over the place, which can become a pretty wearisome tyranny.
Edit: Now you've posted the controller code, it doesn't appear that Model can ever be null anyway, so this seems like a distraction. I suspect you're looking in the wrong place for whatever the problem is.
Edit2: Oh, now it's gone again from the controller code. Feels like a bit of systematic use of the debugger might be more effective than StackOverflow... I would start by passing something fixed, and known into the View call in the controller and then working out what's going on, perhaps by displaying things in your view.
If you're in control of the Bed class, you could help yourself and your successors by adding a HasInfection property to the Bed, which does whatever it needs to to return that boolean. You could similarly improve the controller by moving that BedStatus check into a property. The benefit of this would not merely be stylistic, it would help debug this sort of problem, because stuff relating to the bed object would be happening in that code, and the only stuff left in the view (a horrible place to be debugging) would be much simpler.
HasInfection = Model != null && !string.IsNullOrWhitespace(Model.Infection);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With