19

I have the following line of code:

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

Sometimes this generates 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?

4
  • 2
    can't lkuDomkainType be null itself? When you call lkuDomainType.EditValue , it throws the NullReferenceException. Commented Aug 11, 2010 at 15:03
  • 2
    Because of your title: a == b is not the same as a.Equals(b). Commented Aug 11, 2010 at 15:12
  • @Stefan: Great point! Changed to be more accurate for future searchers...Thanks Commented Aug 11, 2010 at 15:15
  • Possible duplicate of What is a NullReferenceException, and how do I fix it? Commented Sep 4, 2017 at 15:01

10 Answers 10

32

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, seems pretty obvious in hindsight, definitely a 'duh' moment. Thanks.
+2 (unfortunately not possible) for the correct answer and the recommendation to use ??.
+1: The null coalescing (or ??) operator was a great addition to .NET and something I sorely miss when using Java.
16

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;

5 Comments

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
Great answer, +1, but SO says that @Yuriy's answer was first. Thanks though.
@Refracted Yes it was, by 4 seconds :) I like his/her suggestion of using ??
@Refracted Yes, you're right. I've forgot to mention that I upvoted Yuriy's answer, too, for using the ?? operator
In this case, it's Object.Property.Method, and if either Object or Property are undefined, you'd be dereferencing a null pointer.
5

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;

Comments

3

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

lkuDomainType.EditValue == null

Comments

3

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.

Comments

2

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.

Comments

2

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.

2 Comments

I don't understand, isn't String.IsNullOrEmpty also a method?
It's a static method, it's not a method of the lkuDomainType.EditValue object.
2

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;

Comments

1

All current answers fail to address a critical point: the difference between calling Equals on a Nullable type (which is a struct) vs. class type.

Calling Equals on a Nullable typed object whose value is null will not issue an exception, while doing the same on a class typed object will.

Consider the following code:

int? num = null; // int? is Nullable<int>
String s = null; // String is a class

var equalsNullable = num.Equals(null); // works
var equalsClass = s.Equals(null);      // throws

The reason is that Nullable's HasValue method is called in such case, see Jon Skeet's answer for more info.

As other people said, the way to go in the OP's case is to use == or ??.

Comments

0

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.

Comments

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.