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 isnull
Returns:
the result of callingtoString
on the first argument if it is notnull
and the second argument otherwise.See Also:
toString(Object)