3

I wrote this line of code to handle nulls but I still get an "Object reference not set to an instance of an object" error whenever I run this line of code when expectedItem is null. What gives? What's the proper way to write this? Since expectedItem is null, I'd expect expectedItem.ExpectedResultAmount to be null also so this statement should assign an empty string to x.

string x = expectedItem.ExpectedResultAmount != null ? expectedItem.ExpectedResultAmount : "";

9 Answers 9

3

You need to check if expectedItem is not null, not its property

string x = expectedItem != null ? expectedItem.ExpectedResultAmount : "";
Sign up to request clarification or add additional context in comments.

2 Comments

should the op not check both the object instance and its property? see my answer.
@activwerx: if they should - they will find this out soon ;-) Doing mistakes is the best way to learn
1

You should check both the object instance (expectedItem) and the property (expectedItem.ExpectedResultAmount) as either may fail:

string x = expectedItem != null && expectedItem.ExpectedResultAmount != null ? expectedItem.ExpectedResultAmount : "";

6 Comments

I'm picking this one because it checks both the object and its property and I like this syntax style
really? I find reading multiple nested ternary operators hard on the eyes.
@FlavorScape, I agree, but I wrote it in the same fashion as the OP posted in the question.
@FlavorScape, Which of the styles in the answers would you use or do you have a specific style to share? Thanks in advance
@activwerx Which of the styles in the answers would you use or do you have a specific style to share? Thanks in advance
|
1

Your expectation is wrong. try

string x = expectedItem == null ? "" : expectedItem.ExpectedResultAmount != null ? expectedItem.ExpectedResultAmount : "";

1 Comment

+1 same as the selected answer, and you were first by several minutes
1

expcetedItem is no doubt null:

string x;

if (expectedItem != null)
    x = expectedItem.ExpectedResultAmount != null ? expectedItem.ExpectedResultAmount : "";

Comments

1

The short answer: you're dereferencing expectedItem because you're checking expectedItem.ExpectedResultAmount for null rather than checking expectedItem itself. You should probably write

string x = expectedItem != null ? expectedItem.ExpectedResultAmount : "";

Comments

0

expectedItem is null.

if( expectedItem != null)
  x = expectedItem.ExpectedResultAmount != null ? expectedItem.ExpectedResultAmount : "";
else 
  x = "poop";

1 Comment

You can't say if (expecteditem) in C#; you have to explicitly compare it to null.
0

Try this

string x = expectedItem != null && expectedItem.ExpectedResultAmount != null
    ? expectedItem.ExpectedResultAmount
    : string.Empty;

Comments

0

The right way to write your code is as follows:

string x = expectedItem != null ? (expectedItem.ExpectedResultAmount != null ? expectedItem.ExpectedResultAmount : "") : "";

You first have to make sure expectedItem is not null, if it is you would have an illegal operation called null pointer; you should then assign x an empty string.

If it is not null, and you write it like this

string x = expectedItem != null ? expectedItem.ExpectedResultAmount : "";

x could be either null or expectedItem.ExpectedResultAmount, if you don't want x to be null, for instance, you would make string operations on it later such as Compare or Concat, you should also make sure its member ExpectedResultAmount is not null either.

Comments

0

As it has been suggested the the accepted answer is "not easy on the eye", you might also consider this:

string x = ""; //string is empty...
if (expectedItem != null && expectedItem.ExpectedResultAmount != null)
{
    x = expectedItem.ExpectedResultAmount; //...unless this exists.
}

1 Comment

It may be a 5 line (as opposed to my 1 line) solution, but it's very easy to read!

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.