Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any simple way to test null before convert an object to string

Tags:

java

null

I always write

Object o;
if (o!=null)
String s = o.toString();

If there simple way to handle this case?

like image 966
user496949 Avatar asked Apr 02 '11 08:04

user496949


People also ask

How do you check if an object is null?

Typically, you'll check for null using the triple equality operator ( === or !== ), also known as the strict equality operator, to be sure that the value in question is definitely not null: object !== null . That code checks that the variable object does not have the value null .

Can we convert null to string?

Strings are reference types. The built-in Convert. ToString(Object) method converts both null and DBNull to an empty string.

Can null convert to string in Java?

When assigning the null to the string variable, the reference variable does not refer to any memory location in a heap. The null string means no string at all. It does not have a length because it's not a string at all. Applying any standard string operation to the null string will cause a NullPointerException runtime.


9 Answers

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);
like image 123
mmccomb Avatar answered Oct 05 '22 10:10

mmccomb


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"
like image 45
Chris Lindsey Avatar answered Oct 05 '22 11:10

Chris Lindsey


ObjectUtils.toString(object) from commons-lang. The code there is actually one line:

return obj == null ? "" : obj.toString();

Just one note - use toString() only for debug and logging. Don't rely on the format of toString().

like image 43
Bozho Avatar answered Oct 05 '22 11:10

Bozho


Updated answer of Bozho and January-59 for reference:

ObjectUtils.toString(object) from commons-lang. The code there is actually one line:

return obj == null ? "" : obj.toString();

However this method in Apache Commons is now deprecated since release 3.2 (commons/lang3). Their goal here was to remove all the methods that are available in Jdk7. The method has been replaced by java.util.Objects.toString(Object) in Java 7 and will probably be removed in future releases. After some discussion they did not remove it yet (currently in 3.4) and it is still available as a deprecated method.

Note however that said java 7+ method will return "null" for null references, while this method returns and empty String. To preserve behavior use

java.util.Objects.toString(myObject, "")
like image 37
Nick Vanderhoven Avatar answered Oct 05 '22 10:10

Nick Vanderhoven


import java.util.Objects;
Objects.toString(object, "")

Simple and 0 runtime exception :)

like image 35
user881703 Avatar answered Oct 05 '22 12:10

user881703


You can use java 8 Optional in the following way:

String s = Optional.ofNullable(o).map(Object::toString).orElse("null");
like image 26
George Avatar answered Oct 05 '22 12:10

George


String.valueOf(o) returns the string "null" if o is null.

Another one could be String s = o == null? "default" : o.toString()

like image 30
Thorbjørn Ravn Andersen Avatar answered Oct 05 '22 10:10

Thorbjørn Ravn Andersen


Depending on what you want it to on a null value but you can just do this

Object o =
String s = ""+o;

This is the default behaviour for println and string append etc.

like image 25
Peter Lawrey Avatar answered Oct 05 '22 11:10

Peter Lawrey


It looks like you want get a String when o is not a NULL. But I'm confusing that if coding like yours you can't access variable s (you know s in scope of if statement).

like image 38
Pikaurd Avatar answered Oct 05 '22 12:10

Pikaurd