CONNECTING SYSTEMS TO POWER DIGITALIZATION
PORTFOLIO / 2014 — 2025

AVAILABLE FOR PROJECTS
IN JANUARY 2025

me@dassiorleando.xyz
+1 (819) 446 0462
QC, Canada

CONNECTING SYSTEMS TO POWER DIGITALIZATION
me@dassiorleando.xyz
+1 (819) 446 0462
QC, Canada

Safely tostring a java object

Overview

In this little article, we will see how to get the string representation(tostring) of an object cautiously, that means avoiding NullPointerException.

Context

Getting the String representation of a Java object is something we often do for logging, debugging, or simply for overview purposes.

For a null object, we will be directed to a NullPointerException, as the toString is using the fields of the object.

A catch-up of this without even checking for nullity before is just by using the String.valueOf(object) method instead of directly using object.toString().

Here is the content of String.valueOf generated by IntelliJ:

/**
 * Returns the string representation of the {@code Object} argument.
 *
 * @param   obj   an {@code Object}.
 * @return  if the argument is {@code null}, then a string equal to
 *          {@code "null"}; otherwise, the value of
 *          {@code obj.toString()} is returned.
 * @see     java.lang.Object#toString()
 */
public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
}

Where it’s clearly described how the null is catches on before the toString.

This function is also wrapped up into the Objects utility function accessible via the: Objects.toString(Object o), more readable.

A Real Life Example

Let’s suppose your application logic, among others, is using two models with some properties where one depends on the other:

  • ObjectB: propB
  • ObjectA: propA, objectB1 and objectB2

Considering that the dependency objectB2 is optional and somewhere in your code you would like to toString each property of ObjectA(even the optional one).

Here is an approach without the need of checking for nullity:

ObjectB class:

public class ObjectB {
    private String propB;
 
    public ObjectB(String propB) {
        this.propB = propB;
    }
 
    // Getters and setters
 
    @Override
    public String toString() {
        return "ObjectB{" +
                "propB='" + propB + '\'' +
                '}';
    }
}

ObjectA class has objectB2 as an optional property:

public class ObjectB {
    private String propB;
 
    public ObjectB(String propB) {
        this.propB = propB;
    }
 
    // Getters and setters
 
    @Override
    public String toString() {
        return "ObjectB{" +
                "propB='" + propB + '\'' +
                '}';
    }
}

The following JUnit test illustrates how we now proceed to come out with a safe string representation of ObjectA’s instances:

@Test
public void safeToStringTest() {
    ObjectB objectB = new ObjectB("Fake propB");
    ObjectA objectA = new ObjectA("Fake propA", objectB);

    String stringObjectA = Objects.toString(objectA);
    System.out.println(stringObjectA);

    assertNull(objectA.getObjectB2());
    assertTrue(stringObjectA.contains("objectB2=null"));
}

The printed string of our ObjectA into the console is:

ObjectA{propA='Fake propA', objectB1=ObjectB{propB='Fake propB'}, objectB2=null}

More usages of the Objects class can be found on this page.

Conclusion

To sum up, we have seen here a proper way to safely toString an object with embed properties.

As usual, the code is available in the GitHub repo.

Author

Orléando Dassi

I'm a Solutions Architect with 10 years of experience who is constantly learning while impacting the community by producing technical articles/videos, building projects, and conducting tech mentoring/coaching sessions. What describes me the most is my flexibility.