starvingPhilosopher
starvingPhilosopher

Reputation: 47

Why doesn't this ?: operator return null?

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 : "";

Upvotes: 3

Views: 147

Answers (9)

Matthew Layton
Matthew Layton

Reputation: 42260

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.
}

Upvotes: 0

Lele
Lele

Reputation: 1

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.

Upvotes: 0

Matthew Layton
Matthew Layton

Reputation: 42260

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 : "";

Upvotes: 1

Nicodemeus
Nicodemeus

Reputation: 4083

Try this

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

Upvotes: 0

FlavorScape
FlavorScape

Reputation: 14289

expectedItem is null.

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

Upvotes: 0

Adam Mihalcin
Adam Mihalcin

Reputation: 14458

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 : "";

Upvotes: 1

Simon Whitehead
Simon Whitehead

Reputation: 65079

expcetedItem is no doubt null:

string x;

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

Upvotes: 1

D Stanley
D Stanley

Reputation: 152556

Your expectation is wrong. try

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

Upvotes: 1

zerkms
zerkms

Reputation: 254916

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

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

Upvotes: 3

Related Questions