Outputting a null string with Apex

What would you expect a null String to convert to output? It makes sense if null was output as ”, or a blank String, right?

Unfortunately it actually outputs as ‘null’. Ridiculous, right?

System.debug((String)null);
10:32:58.032|USER_DEBUG|[1]|DEBUG|null

Consider this dumb utility function to circumvent that:

// dumb utility function to output '' instead of 'null'
// because (String)null == 'null', not '';
private String s(String input) {
	return input == null ? '' : input;
}

Fixed:

private String s(String input) {
	return input == null ? '' : input;
}
System.debug(s((String)null));
10:35:55.037|USER_DEBUG|[4]|DEBUG|
This entry was tagged , . Bookmark the permalink.

One Response to Outputting a null string with Apex

  1. Tony Coleman says:

    I agree with your comments on the returned value. Thanks for sharing.

Comments are closed.