Instead of catching the exception or putting conditions, use
String.valueOf(result.getPropertyAsString(0));
It will call toString() method of the argument and will convert it to String and if result.getPropertyAsString(0) is null, then it will change it to "null".
Answer from Prashant K on Stack Overflowjava - Convert null object to String - Stack Overflow
java - Convert null string to "" - Stack Overflow
java - Get empty string when null - Stack Overflow
java - How to set null value if String is empty? - Stack Overflow
Instead of catching the exception or putting conditions, use
String.valueOf(result.getPropertyAsString(0));
It will call toString() method of the argument and will convert it to String and if result.getPropertyAsString(0) is null, then it will change it to "null".
Although it's not a good practice, you can concatenate the null value with "" to make it a String.
For example:
String str=null;
System.out.println((str+"").length()); /// prints 4
You could try it like this using the ternary operator.
System.out.println(a == null ? "" : a);
Alernatively, you can use the Commons Lang3 function defaultString() as suggested by chrylis,
System.out.println(StringUtils.defaultString(a));
I would factor out a utility method that captures the intention, which improves readability and allows easy reuse:
public static String blankIfNull(String s) {
return s == null ? "" : s;
}
Then use that when needed:
System.out.println(blankIfNull(a));
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
toStringon 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 isnullReturns:
the result of callingtoStringon the first argument if it is notnulland the second argument otherwise.See Also:
toString(Object)
For java 8 you can use Optional approach:
Optional.ofNullable(gearBox).orElse("");
Optional.ofNullable(id).orElse("");
Another alternative using Guava's emptyToNull:
text = Strings.emptyToNull(text);
I don't know the context in which you thought to mention streams relating to your question, but if you are open to the Apache StringUtils library, then one option would be to use the StringUtils#isEmpty() method:
if (StringUtils.isEmpty(text)) {
text = null;
}
Assign null
When we write:
String s = "Hello World!";
s = null;
The String object still exists in memory because this does not delete it. However the garbage collector will clear the object from memory as there is no variable referencing it.
For all practical purposes s = null; deletes the String.
tl;dr
set a String to null in Java?
myString = null ;
declare the variable without initializing it
Uninitialized deaults to null, no String object. Empty reference variable.
String myString ;
You can explicitly assign null. Same effect as line above. No object, empty reference.
String name = null;
Declare and initialize to empty string:
String myString = "" ; // A `String` object containing no characters. Not null.
Details
Before posting here on basic Java Questions, study the Java Tutorials provided by Oracle free of cost.
See the tutorial page on string literals. To quote:
There's also a special
nullliteral that can be used as a value for any reference type.nullmay be assigned to any variable, except variables of primitive types. There's little you can do with anullvalue beyond testing for its presence. Therefore,nullis often used in programs as a marker to indicate that some object is unavailable.
👉 So null is not a piece of text with four characters. The keyword null means “no object at all”, no String, nothing at all, an empty reference.
To test if an object reference variable is null (contains no reference), you have a few choices:
Objects.isNull( myVar )andObjects.nonNull( myVar )null == myVarandnull != myVarmyVar == nullandmyVar != null
I prefer the first, as words are easier to read than mathematical-like symbols.
The static valueOf method in the String class will do the null check and return "null" if the object is null:
String stringRepresentation = String.valueOf(o);
Try Objects.toString(Object o, String nullDefault)
Example:
import java.util.Objects;
Object o1 = null;
Object o2 = "aString";
String s;
s = Objects.toString(o1, "isNull"); // returns "isNull"
s = Objects.toString(o2, "isNull"); // returns "aString"