Get empty string when null

JavaGuava

Java Problem Overview


I want to get string values of my fields (they can be type of long string or any object),

if a field is null then it should return empty string, I did this with guava;

nullToEmpty(String.valueOf(gearBox))
nullToEmpty(String.valueOf(id))
...

But this returns null if gearbox is null! Not empty string because valueOf methdod returns string "null" which leads to errors.

Any Ideas?

EDIt: there are 100s fields I look for something easy to implement

Java Solutions


Solution 1 - Java

You can use Objects.toString() (standard in Java 7):

Objects.toString(gearBox, "")

Objects.toString(id, "")

From the linked documentation:

>public static String toString(Object o, String nullDefault)

> >Returns the result of calling toString on the first argument if the first argument is not null and returns the second argument otherwise.

> >Parameters:
o - an object
nullDefault - string to return if the first argument is null

> >Returns:
the result of calling toString on the first argument if it is not null and the second argument otherwise.

> >See Also:
toString(Object)

Solution 2 - Java

For java 8 you can use Optional approach:

Optional.ofNullable(gearBox).orElse("");
Optional.ofNullable(id).orElse("");

Solution 3 - Java

If you don't mind using Apache commons, they have a StringUtils.defaultString(String str) that does this.

> Returns either the passed in String, or if the String is null, an empty String ("").

If you also want to get rid of "null", you can do:

StringUtils.defaultString(str).replaceAll("^null$", "")

or to ignore case:

StringUtils.defaultString(str).replaceAll("^(?i)null$", "")

Solution 4 - Java

If alternative way, Guava provides Strings.nullToEmpty(String).

Source code

String str = null;
str = Strings.nullToEmpty(str);
System.out.println("String length : " + str.length());

Result

0

Solution 5 - Java

Use an inline null check

gearBox == null ? "" : String.valueOf(gearBox);

Solution 6 - Java

StringUtils.defaultString(String str) Returns either the passed in String, or if the String is null, an empty String ("").

Example from java doc > StringUtils.defaultString(null) will return "" > StringUtils.defaultString("") will return "" > StringUtils.defaultString("bat") will return "bat"

Solution 7 - Java

Since you're using guava:

Objects.firstNonNull(gearBox, "").toString();

Solution 8 - Java

In Java 9+ use : Objects.requireNonNullElse (obj, defaultObj) https://docs.oracle.com/javase/9/docs/api/java/util/Objects.html#requireNonNullElse-T-T-

//-- returns empty string if obj is null
Objects.requireNonNullElse (obj, "")   

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSpringView Question on Stackoverflow
Solution 1 - JavaarshajiiView Answer on Stackoverflow
Solution 2 - JavaFederico PiazzaView Answer on Stackoverflow
Solution 3 - JavaKeppilView Answer on Stackoverflow
Solution 4 - JavaWon-Sik KimView Answer on Stackoverflow
Solution 5 - JavaSamhainView Answer on Stackoverflow
Solution 6 - JavaAkshay SardharaView Answer on Stackoverflow
Solution 7 - JavarenkeView Answer on Stackoverflow
Solution 8 - JavaAri SinghView Answer on Stackoverflow