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)
"Sand".Equals(p.MineralName)?p.MineralName == "Sand"Equals()function, it's merely the consequence of trying to access anything from anullvalue. 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 ofp.MineralName, not for the algorithm of.Equals(). You're "blaming" the wrong party for the exception.MineralNameis of type string, but it has the value ofnull. Also, theEquals()you used is not static.String.Equals(foo,bar)is static.foo.Equals(bar)is not. Statics are of the formClassName.MethodName(), notvariableName.MethodName().