Consider the code written when a NullReferenceException occurs in the process of assigning a value to a spread cell. I added the Null check without thinking deeply, but there are a lot of things to think about.
A code that displays the value obtained from the DB in the spread, and the value may or may not be null. Spreads are third-party products, so we're replacing them with our own class. One day, a NullReferenceException occurred at the point where a value was assigned to a cell.
        public void Main(SpreadCell cell)
        {
            int? dbValue = null;
            //The process of getting the value from the DB is omitted.
            //NullReferenceException occurs.
            cell.Value = dbValue.Value;
            
        }
        /**Spread cell object**/
        private class SpreadCell
        {
            public Object Value { get; set; }
        }
    public void main(SpreadCell cell) {
        Integer dbValue = null;
        //The process of getting the value from the DB is omitted.
        //NullPointErexception occurs.
        cell.setValue(dbValue.intValue());
    }
I'm adding Null checks without thinking, and there's no else. Notice that you don't need a null check above all else.
        public void Main(SpreadCell cell)
        {
            int? dbValue = null;
            //The process of getting the value from the DB is omitted.
            //Assign a value only if it is not Null.
            if (dbValue != null)
            {
                cell.Value = dbValue.Value;
            }          
        }
    public void main(SpreadCell cell) {
        Integer dbValue = null;
        //The process of getting the value from the DB is omitted.
        //Assign a value only if it is not Null.
        if (dbValue != null) {
            cell.setValue(dbValue.intValue());
        }
    }
There is no problem if you substitute the nullable type as it is. If you take a hundred steps and do a null check, put in the cell initialization code.
        public void Main(SpreadCell cell)
        {
            int? dbValue = null;
            //The process of getting the value from the DB is omitted.
            //You can substitute it as it is.
            cell.Value = dbValue;
            //If you use an if statement, please initialize it.
            cell.Value = null;
            if (dbValue != null)
            {
                cell.Value = dbValue.Value;
            }
        }
    public void main(SpreadCell cell) {
        Integer dbValue = null;
        //The process of getting the value from the DB is omitted.
        //You can substitute it as it is.
        cell.setValue(dbValue);
        //If you use an if statement, please initialize it.
        cell.setValue(null);
        if (dbValue != null) {
            cell.setValue(dbValue.intValue());
        }
    }
You shouldn't add a null check without thinking just because a NullReferenceException has occurred. If you need a null check, don't forget the initialization code.
For those who deal with these obstacles, I'm worried that other codes have been fixed properly.
Previous article (useless processing for collection)
[Next article (Handling of Bool type)] (http://qiita.com/csharpisthebest/items/b0c44764948e1334e94a)
Recommended Posts