3

I ran into a null reference exception on line:

dr["IsSandpit"] = p.MineralName.Equals("Sand");

Of course the fix is:

dr["IsSandpit"] = p.MineralName!=null && p.MineralName.Equals("Sand");

But I am wondering if a nullable type can be configured that the Equals method instead of throwing a null reference exception would just return false for a strongly typed variable? Or some method NotNullAndEquals?

Null in the database means something like "not filled" or empty and "empty" surely is not equal to "Sand". (Perhaps this is a duplicate but I have not found the right question)

16
  • 8
    "Sand".Equals(p.MineralName)? Commented Feb 19, 2018 at 9:37
  • 4
    p.MineralName == "Sand" Commented Feb 19, 2018 at 9:37
  • 3
    Your interpretation of the issue is slightly off, which is changing your expectations. The exception is not thrown inside the Equals() function, it's merely the consequence of trying to access anything from a null value. Whether you were trying to access a property, method, or field is irrelevant, the NRE will be thrown regardless. If you want to implement a workaround here, it should be implemented for the value of p.MineralName, not for the algorithm of .Equals(). You're "blaming" the wrong party for the exception. Commented Feb 19, 2018 at 9:38
  • 1
    string.Equals(p.MineralName, "Sand") Commented Feb 19, 2018 at 9:44
  • 2
    @VojtěchDohnal: MineralName is of type string, but it has the value of null. Also, the Equals() you used is not static. String.Equals(foo,bar) is static. foo.Equals(bar) is not. Statics are of the form ClassName.MethodName(), not variableName.MethodName(). Commented Feb 19, 2018 at 9:46

3 Answers 3

5

You can call the static Equals method on both operands:

var isEqual =  string.Equals("Sand", p.MineralName); // return false for null.

This will remove the need to access a potentially null reference. Alternately, you can use the null propagation operator to avoid the operation if the value is null:

var isEqual = p.MineralName?.Equals("Sand"); // return null for null.

These are much better approaches, IMO, than using extension methods to hack together a method that can theoretically be called on a null reference, since that approach leads to confusion on whether you can safely call a method without checking for null. Imagine this code:

 if (!p.MineralName.Equals("Sand"))
 {  
       return p.MineralName.ToUpper(); // throws NullReferencException!
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Does the null propagation approach not mean that the resulting value is null, rather than false? This would implicitly make var isEqual of type int? rather than int.
" return false for null." - not true, it returns null for null
0

The expression

p.MineralName?.Equals("Sand")

will evaluate to null if p.MineralName is null. You can then coalesce it to false, like so:

p.MineralName?.Equals("Sand") ?? false

Comments

-1

As an option you can utilize a self-created NotNullAndEquals extension method:

    public static class Utilities {
        public static bool NotNullAndEquals(this string value, string expected) {
            return value != null && value.Equals(expected);
        }
    }

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.