views:

98

answers:

5

Hi -

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.getAbout());
    results.add(usr.getBirthday());
    results.add(usr.getEmail());
    results.add(usr.getGender());
A: 

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 :

Colin Hebert
+4  A: 

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}" ?: ""
}
Jeff Storey
Ah I dont want to have an if statement for each individual string to see if its there or not. I find it kind of dirty to read. But if its the only option then I;ll have to use that
steve
@Steve, as others stated though, you could always remove the nulls at the end (but I prefer not to have nulls in the list at all).
Jeff Storey
@Steve, also see Edit #1 for some other possibilities.
Jeff Storey
A: 

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");
Justin Niessner
A: 

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.

Carl Manaster
+1  A: 

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; }
Peter Tillemans
Note that `default` is a reserved keyword in Java (used in `switch` statements).
BalusC
Oops.... thanks for point it out. I fixed it. I cannot remember when I last used a switch statement....
Peter Tillemans
public static <T> T getOrElse(T t, T defaultValue) { return t == null ? defaultValue : d;}-- No need to repeat yourself.
Ricky Clarkson
Package names should be all lowercase in Java.
Steve Kuo
@Ricky Clarkson Thats the nice one. I also thought the same solution. I think you can post that as an answer.
Marimuthu Madasamy