commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Henning P. Schmiedehausen" <...@intermeta.de>
Subject Re: [LANG] Do we have a safeToString() ?
Date Tue, 05 Aug 2003 14:42:58 GMT
"Brian S O'Neill" <broneill@earthlink.net> writes:

Ah right. Thanks, that's exactly what I'm chasing after.

	Regards
		Henning


>The safe way to call toString and avoid a NPE is to call
>String.valueOf(xxx). If the underlying toString method throws some other
>exception internally, that exception will still get thrown.

>----- Original Message ----- 
>From: "Henning P. Schmiedehausen" <hps@intermeta.de>
>Newsgroups: hometree.jakarta.commons.dev
>To: <commons-dev@jakarta.apache.org>
>Sent: Tuesday, August 05, 2003 05:04 A
>Subject: Re: [LANG] Do we have a safeToString() ?


>> scolebourne@btopenworld.com writes:
>>
>> >StringUtils.defaultString()
>>
>> Takes a String, not an Object.
>>
>> >ObjectUtils.toString()
>>
>> What? This might return org.apache.commons.lang.ObjectUtils@somewhere in
>my memory.
>>
>> I want to call toString() on arbitrary objects and avoid the NPE when I do
><foo>.toString().
>>
>> Regards
>> Henning
>>
>>
>>
>> >Stephen
>>
>> >>  from:    "Henning P. Schmiedehausen" <hps@intermeta.de>
>> >> as I was needing a method like this:
>> >>
>> >> /**
>> >>  * Returns the toString() value of the passed
>> >>  * object. If null is passed, return the String
>> >>  * "null".
>> >>  *
>> >>  * @param obj The object to print out.
>> >>  * @returns A String representation of the passed
>> >>  *          object.
>> >>  */
>> >> public String safeToString(Object obj) {
>> >>     if (obj == null) {
>> >>         return "null";
>> >>     }
>> >>     return obj.toString();
>> >> }
>> >>
>> >> for the gadzillionth time and we have
>> >>
>> >>     public static String identityToString(Object object) {
>> >>         if (object == null) {
>> >>             return null;
>> >>         }
>> >>         return new StringBuffer()
>> >>             .append(object.getClass().getName())
>> >>             .append('@')
>> >>
>.append(Integer.toHexString(System.identityHashCode(object)))
>> >>             .toString();
>> >>     }
>> >>
>> >> in ObjectUtils, wouldn't this safeToString() be a nice addition to the
>> >> ObjectUtils? Or do we already have something like this in the lang
>> >> somewhere?
>> >>
>> >> Regards
>> >> Henning
>> >> -- 
>> >> Dipl.-Inf. (Univ.) Henning P. Schmiedehausen          INTERMETA GmbH
>> >> hps@intermeta.de         49 9131 50 654 0   http://www.intermeta.de/
>> >>
>> >> Java, perl, Solaris, Linux, xSP Consulting, Web Services
>> >> freelance consultant -- Jakarta Turbine Development  -- hero for hire
>> >>
>> >> "You are being far too rational for this discussion."
>> >>        --- Scott Robert Ladd in <3F1874B0.6030507@coyotegulch.com>
>> >>
>> >> ---------------------------------------------------------------------
>> >> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
>> >> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>> >>
>>
>>
>> >---------------------------------------------------------------------
>> >To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
>> >For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>>
>> -- 
>> Dipl.-Inf. (Univ.) Henning P. Schmiedehausen          INTERMETA GmbH
>> hps@intermeta.de        +49 9131 50 654 0   http://www.intermeta.de/
>>
>> Java, perl, Solaris, Linux, xSP Consulting, Web Services
>> freelance consultant -- Jakarta Turbine Development  -- hero for hire
>>
>> "You are being far too rational for this discussion."
>>        --- Scott Robert Ladd in <3F1874B0.6030507@coyotegulch.com>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>>


>---------------------------------------------------------------------
>To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: commons-dev-help@jakarta.apache.org

-- 
Dipl.-Inf. (Univ.) Henning P. Schmiedehausen          INTERMETA GmbH
hps@intermeta.de        +49 9131 50 654 0   http://www.intermeta.de/

Java, perl, Solaris, Linux, xSP Consulting, Web Services 
freelance consultant -- Jakarta Turbine Development  -- hero for hire

"You are being far too rational for this discussion."  
       --- Scott Robert Ladd in <3F1874B0.6030507@coyotegulch.com>

Mime
View raw message