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?

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Refracted Paladin
  • 12,096
  • 33
  • 123
  • 233

10 Answers10

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.

Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142
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;
NullUserException
  • 83,810
  • 28
  • 209
  • 234
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;
Jerod Houghtelling
  • 4,783
  • 1
  • 22
  • 30
3

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

lkuDomainType.EditValue == null
Ray
  • 21,485
  • 5
  • 48
  • 64
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.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
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.

GWLlosa
  • 23,995
  • 17
  • 79
  • 116
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.

brendan
  • 29,308
  • 20
  • 68
  • 109
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;
Rony
  • 9,331
  • 2
  • 22
  • 22
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 ??.

OfirD
  • 9,442
  • 5
  • 47
  • 90
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.

Russ
  • 4,091
  • 21
  • 32