开发者

Clean way of handling null pointers when using arrays

I have the following code which is adding somestrings to an arraylist. It will ever so often have an empty variable due to someone not filling it in correctly. I don't own the usr object so I can't modify it unfortunately. Is there a clean and easy way of just adding a default value if one of these values is empty? I don't mind if its empty, but I don't want the program to crash out!

    results.add(usr.getName());
    results.add(usr.get开发者_StackOverflow社区About());
    results.add(usr.getBirthday());
    results.add(usr.getEmail());
    results.add(usr.getGender());


You just have to check if the values are null.

String name = usr.getName();
if ( name != null ) {
   results.add(name);
}

You can do this for each of the values. You can use a shorter syntax like

results.add(usr.getName() != null ? usr.getName() : "");

Though that requires calling getName twice, it shouldn't matter since I assume that is just a simple getter.

Edit #1

If you don't want to check for null on every check, you can use a reflection based solution. This example is groovy based, and while I do think it's overkill for a few values, if you have a lot of values, it might make more sense.

results.add(getValue(usr,"name"))
results.add(getValue(usr,"about"))

String getValue(def usr, String prop) {
   return usr."${prop}" ?: ""
}


If you need this often you could make a Cleaner class like :

package com.acme.util;

class Cleaner {

    public static String clean(String s, String defaultValue) {
        return s == null ? defaultValue : s;
    }

    public static Integer clean(Integer v, Integer defaultValue) {
        return v == null ? defaultValue : v;
    }

    public static Date clean(Date d, Date defaultValue) {
        return v == null ? defaultValue : d;
    }

    // ... and so on ...

}

and then use is as :

import static com.acme.util.Cleaner.*;

results.add(clean(usr.getName(),"John Doe"));
...

I do not like static imports but I like null checks everywhere even less.

Ricky Clarkson pointed out this can be expressed even shorter using generics as :

public static <T> T getOrElse(T t, T defaultValue) { return t == null ? defaultValue : t; }


Check for a null return value inline and then replace the null with a default value all using the conditional operator:

results.add(usr.getName() != null ? usr.getName() : "default");


If you want a List that reads null as - say - the empty string, you could write a class that implements List and delegates most every call to an internal ArrayList, but in the case of add(), first checks for null. You could also just subclass ArrayList, but that's generally considered a bad idea.


It seems that you don't want theses values at all, and having an empty string doesn't lead to anything. If you just want to have a null-free List, either you check each value to avoid any null value in your List, or you can remove any null value in your array after every value has been added :

result.remove(null);

An even better thing to do, is understand why your program craches. Maybe you could check for null values when you read everything in your List.


Resources :

  • Javadoc - List.remove()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜