views:

301

answers:

9

I have the following line of code -->

var selectedDomainID = lkuDomainType.EditValue.Equals(null) ? string.Empty : lkuDomainType.EditValue;

that is, sometimes, generating a NullReferenceException What I don't understand is why. Isn't the whole point of my code to check for null and if so assign string.empty?? When I check in DEBUG it is stating that EditValue == null so what am I missing?

Thanks

+15  A: 

Use lkuDomainType.EditValue == null, otherwise you are trying to call an instance method on a null object. But the better option might be lkuDomainType.EditValue ?? String.Empty. Also watch out for lkuDomainType being null, unless it is a class not an object.

Yuriy Faktorovich
Thanks, seems pretty obvious in hindsight, definitely a 'duh' moment. Thanks.
Refracted Paladin
+2 (unfortunately not possible) for the correct answer and the recommendation to use `??`.
Stefan Steinegger
+1: The null coalescing (or `??`) operator was a great addition to .NET and something I sorely miss when using Java.
R. Bemrose
+10  A: 

When you use Object.Property and Object is undefined, you are dereferencing a null pointer and that's why you get the exception. Instead, use:

var selectedDomainID = lkuDomainType.EditValue == null ? string.Empty : lkuDomainType.EditValue;
NullUserException
I had to upvote this answer a) for the correct answer, b) for using *dereference* in the sentence, and b) for the almost appropriate user name
Giu
Great answer, +1, but SO says that @Yuriy's answer was first. Thanks though.
Refracted Paladin
@Refracted Yes it was, by 4 seconds :) I like his/her suggestion of using `??`
NullUserException
@Refracted Yes, you're right. I've forgot to mention that I upvoted Yuriy's answer, too, for using the `??` operator
Giu
In this case, it's `Object.Property.Method`, and if either `Object` or `Property` are undefined, you'd be dereferencing a null pointer.
R. Bemrose
+3  A: 

you are trying to call the Equals method on a null object - do it like this instead:

lkuDomainType.EditValue == null
Ray
+2  A: 

EditValue == null. That means that there is no object there. You cannot call functions on null objects, even if the function is .Equals().

You're better off just saying "(lkuDomainType.EditValue == null)" in this case.

GWLlosa
+5  A: 

If EditValue is null then you can't call Equals. In this cas you would have to do:

var selectedDomainID = lkuDomainType.EditValue == null ? string.Empty : lkuDomainType.EditValue;

Or you can simplify it by doing:

var selectedDomainID = lkuDomainType.EditValue ?? string.Empty;
Jerod Houghtelling
+1  A: 

You should use String.IsNullOrEmpty here. Like this:

var selectedDomainID = String.IsNullOrEmpty(lkuDomainType.EditValue) ? string.Empty : lkuDomainType.EditValue;

Equals is a method, you're trying to call a method on a null object which is throwing an exception.

brendan
I don't understand, isn't `String.IsNullOrEmpty` also a *method*?
Refracted Paladin
It's a static method, it's not a method of the lkuDomainType.EditValue object.
brendan
Ah, thanks for clearing it up.
Refracted Paladin
+1  A: 

when EditValue is null you cannot call the Equals method on it so the best way to check is to use

lkuDomainType.EditValue == null ? string.Empty : lkuDomainType.EditValue;
Rony
+3  A: 

The problem is that you are using the object before checking if it's null. You are calling the Equals method of the object, which fails if the reference is null.

You have to exchange your lkuDomainType.EditValue.Equals(null) for lkuDomainType.EditValue == null.

Guffa
A: 

If lkuDomainType.EditValue is null, then "lkuDomainType.EditValue.Equals(someObject)" is the same as coding "null.Equals(someObject)". Well, obviously "null" doesn't have any members or methods (it wouldn't be null if it did). That's why you get a NullReferenceException.

Most of the examples from the other posts will work, including String.IsNullOrEmpty, which is a method that returns a boolean value.

Russ