Click here to Skip to main content
15,887,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a field InstituicaoEnsinoId (int), but sometimes i dont need this information, only if campo1=3 others i need. to capture this id i use:

var instituicaoEnsinoDoOrcamento = models.FirstOrDefault().Documento.InstituicaoEnsinoId.Value;


What I have tried:

i tried use

 if (campo1 == 3) {
          var instituicaoEnsinoDoOrcamento = models.FirstOrDefault().Documento.InstituicaoEnsinoId.Value;
}
else {
     var insituicaoEninoDOOrcamento = 0;
} 

this dos not work.
Posted
Updated 17-Jun-20 12:36pm

1 solution

This happens when your code is written to assume everything works perfectly. It doesn't. What this means is you're trying to either call a method or access a property on an object that doesn't exist, or is null.

This is really easy to diagnose. All you have to do is put a breakpoint on the "return View" where you passed the model to the view that's crashing.

Run the app and get to the point where it crashes and when you get the view returned, it'll stop at the breakpoint. Hover mouse over the model you're returning and you can examine the object and any properties. Either theroot object is null, Documento is null, InstituicaoEnsinoId is null, or you're trying to use the Value and it's null.

Once you know what returns null, you have to go back through your code and figure out why that object is null.

Nobody can do any of this for you. YOU have to use the debugger to figure out what went wrong and where.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900