Link to home
Start Free TrialLog in
Avatar of lulu50
lulu50Flag for United States of America

asked on

How I can handle System.NullReferenceException error?

Hello,

I'm getting this error:

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

@if (Model.Environment.ToString() != " " || !String.IsNullOrEmpty(Model.Environment.ToString())
        || Model.Environment.ToString() != null)
 {
                            @Html.LabelForModel(Model.Environment, new { @style = "font-weight:normal;font-size:12px;" })
                            }

Open in new window

my value for Model.Environment is null
and
I want to say if it is not null then do the @Html.LabelForModel

I have tried everything I can think of but it's not working

Thank you in advance for your help.




ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Checking environment should be enough:
@if (!string.IsNullOrWhiteSpace(Model.Environment))

The model is always valid (!= null) because it is a Razor page.