0

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());
1
  • Not sure, what is the type of results, seems it must be generic Collection. One of the elegant ways to handle nulls is to have your own subclass of List (or other Collection), which will handle adding null's as you wish. Commented Aug 31, 2010 at 17:31

5 Answers 5

4

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}" ?: ""
}
Sign up to request clarification or add additional context in comments.

3 Comments

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, 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).
@Steve, also see Edit #1 for some other possibilities.
1

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; }

5 Comments

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

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");

Comments

0

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.

Comments

0

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 :

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.