Java method to return NULL as an empty String.

What if the String you are expecting comes back as null? Would it be the end of the world for you? Not if you have good exception handling. This method is also useful.

It will return an empty String “” of the String is null. If the String is not null, it will return the trimmed version of the Java String.

 private String getNullAsEmptyString(Object o){

        if( o instanceof String){
            if( o == null){
                return “”;
            }
            return  o.toString().trim();
        }

        return null;
    }

return-null-as-empty-string

Nwankwo Kanu, never known to deal with null Strings.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: