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 OverflowInstead 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
Conversion of null to string in Java - Stack Overflow
java - Convert null string to "" - Stack Overflow
java - Dealing with null-to-string errors - Stack Overflow
Cleanest way to check for null on a String?
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("");
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));
Try using String.valueOf(userDict.get("key1")). According to the specs of valueOf(Object o), it will either call toString() on the object or return the string "null" if given a null Object.
EDIT: edited to point to the java 7 api instead of the java 6 api.
EDIT 2: If you don't mind making your code a bit more dense, you could encapsulate the logic of this method by using the ternary operator:
String value1 = (userDict.get("key1") == null) ? "" : userDict.get("key1").toString();
Perhaps set it to an empty string if it returns null.
That's up to you. It depends what the purpose of this code is.
I'm trying to avoid writing an if statement for each .get()
If you want to avoid repetitive code, then write yourself a helper method.
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"
If I understand correctly, you can use the ternary opperator:
System.out.println("My string is: " + ((string == null) ? "" : string));
In case you are not familiar with it, it reads "Is string null? If it is, then 'return' en empty string, else 'return' string". I say 'return' because you can consider ((string == null) ? "" : string) as a function that returns a String.
You can replace the empty string with any other String of course.
If I understand correctly, you need this
public static String replaceNull(String input) {
return input == null ? "" : input;
}
and the use it where ever you need it like
sheet.addCell(new Label(4,currentRow, replaceNull(a.getParan())+" "+replaceNull(a.getO())+" "+replaceNull(a.getParan())));
Hope this helps