You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by "Henning P. Schmiedehausen" <hp...@intermeta.de> on 2005/08/22 08:32:35 UTC
Re: svn commit: r233505 - in /jakarta/commons/proper/configuration/trunk/src:
oheger@apache.org writes:
> public int hashCode()
> {
>- return keyBuffer.hashCode();
>+ return keyBuffer.toString().hashCode();
> }
This screams "NullPointerException" all over the place.
How about
public int hashCode() {
return String.valueOf(keyBuffer).hashCode();
}
Regards
Henning
--
Dipl.-Inf. (Univ.) Henning P. Schmiedehausen INTERMETA GmbH
hps@intermeta.de +49 9131 50 654 0 http://www.intermeta.de/
RedHat Certified Engineer -- Jakarta Turbine Development -- hero for hire
Linux, Java, perl, Solaris -- Consulting, Training, Development
4 - 8 - 15 - 16 - 23 - 42
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: svn commit: r233505 - in /jakarta/commons/proper/configuration/trunk/src:
Posted by Oliver Heger <ol...@t-online.de>.
Henning P. Schmiedehausen wrote:
>Brett Porter <br...@apache.org> writes:
>
>
>
>>How?
>>
>>
>
>
>
>>if keyBuffer is null (which it isn't, the constructor initialises it),
>>String.valueOf( keyBuffer ) would be an NPE as well. toString() should
>>not return null and won't for a StringBuffer.
>>
>>
>
>What the...?????
>
>--- cut ----
>import java.io.File;
>
>public class test {
> public static void main(String[] args) throws Exception {
> System.out.println(String.valueOf((Object) null));
> }
>}
>--- cut ----
>
>[henning@forge tmp]$ javac test.java
>[henning@forge tmp]$ java -version
>java version "1.5.0_02"
>Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_02-b09)
>Java HotSpot(TM) Client VM (build 1.5.0_02-b09, mixed mode, sharing)
>[henning@forge tmp]$ java test
>Exception in thread "main" java.lang.NullPointerException
> at java.lang.String.<init>(String.java:173)
> at java.lang.String.valueOf(String.java:2591)
> at test.main(test.java:5)
>
>[henning@forge tmp]$ javac test.java
>[henning@forge tmp]$ java -version
>java version "1.4.2_08"
>Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_08-b03)
>Java HotSpot(TM) Client VM (build 1.4.2_08-b03, mixed mode)
>[henning@forge tmp]$ java test
>null
>
>Oh fsck, Sun, please, please, please don't tell me that you screwed
>_that_ one up.....
>
>The javadoc even states:
>
>http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#valueOf(java.lang.Object)
>
>--- cut ---
>valueOf
>
>public static String valueOf(Object obj)
>
> Returns the string representation of the Object argument.
>
> Parameters:
> obj - an Object.
> Returns:
> if the argument is null, then a string equal to "null";
> otherwise, the value of obj.toString() is returned.
> See Also:
> Object.toString()
>
>--- cut ---
>
>Congrats. You found a bug. IMHO.
>
>This is BTW one of my personal mnemonics.
>
>I do replace foobar.toString() with String.valueOf(foobar) because up
>until a few seconds ago I was under the firm impression, that
>String.valueOf(...) could _never_ throw NPE. This is a constant source
>of sorrow with code that I'm working on, because often co-workers tell
>me "that can never be null". Yeah, unless, some weird condition
>happens that was not tested for and surfaced after customer
>installation. Better safe than sorry. Defensive programming... :-)
>
> Regards
> Henning
>
>
>
Despite of your test results I think that String.valueOf() is the safer
variant. So I will change this.
Thanks for noticing.
Oliver
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: svn commit: r233505 - in /jakarta/commons/proper/configuration/trunk/src:
Posted by sebb <se...@gmail.com>.
On 22/08/05, Henning P. Schmiedehausen <hp...@intermeta.de> wrote:
> Brett Porter <br...@apache.org> writes:
>
> >How?
>
> >if keyBuffer is null (which it isn't, the constructor initialises it),
> >String.valueOf( keyBuffer ) would be an NPE as well. toString() should
> >not return null and won't for a StringBuffer.
>
> What the...?????
>
> --- cut ----
> import java.io.File;
>
> public class test {
> public static void main(String[] args) throws Exception {
> System.out.println(String.valueOf((Object) null));
> }
> }
> --- cut ----
>
> [henning@forge tmp]$ javac test.java
> [henning@forge tmp]$ java -version
> java version "1.5.0_02"
> Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_02-b09)
> Java HotSpot(TM) Client VM (build 1.5.0_02-b09, mixed mode, sharing)
> [henning@forge tmp]$ java test
> Exception in thread "main" java.lang.NullPointerException
> at java.lang.String.<init>(String.java:173)
> at java.lang.String.valueOf(String.java:2591)
> at test.main(test.java:5)
>
Seems to be fixed in the latest Windows JVM:
D:\temp>java -version
java version "1.5.0_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
Java HotSpot(TM) Client VM (build 1.5.0_04-b05, mixed mode, sharing)
D:\temp>java test
null
S.
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: svn commit: r233505 - in /jakarta/commons/proper/configuration/trunk/src:
Posted by "Henning P. Schmiedehausen" <hp...@intermeta.de>.
Brett Porter <br...@apache.org> writes:
>How?
>if keyBuffer is null (which it isn't, the constructor initialises it),
>String.valueOf( keyBuffer ) would be an NPE as well. toString() should
>not return null and won't for a StringBuffer.
What the...?????
--- cut ----
import java.io.File;
public class test {
public static void main(String[] args) throws Exception {
System.out.println(String.valueOf((Object) null));
}
}
--- cut ----
[henning@forge tmp]$ javac test.java
[henning@forge tmp]$ java -version
java version "1.5.0_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_02-b09)
Java HotSpot(TM) Client VM (build 1.5.0_02-b09, mixed mode, sharing)
[henning@forge tmp]$ java test
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.<init>(String.java:173)
at java.lang.String.valueOf(String.java:2591)
at test.main(test.java:5)
[henning@forge tmp]$ javac test.java
[henning@forge tmp]$ java -version
java version "1.4.2_08"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_08-b03)
Java HotSpot(TM) Client VM (build 1.4.2_08-b03, mixed mode)
[henning@forge tmp]$ java test
null
Oh fsck, Sun, please, please, please don't tell me that you screwed
_that_ one up.....
The javadoc even states:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#valueOf(java.lang.Object)
--- cut ---
valueOf
public static String valueOf(Object obj)
Returns the string representation of the Object argument.
Parameters:
obj - an Object.
Returns:
if the argument is null, then a string equal to "null";
otherwise, the value of obj.toString() is returned.
See Also:
Object.toString()
--- cut ---
Congrats. You found a bug. IMHO.
This is BTW one of my personal mnemonics.
I do replace foobar.toString() with String.valueOf(foobar) because up
until a few seconds ago I was under the firm impression, that
String.valueOf(...) could _never_ throw NPE. This is a constant source
of sorrow with code that I'm working on, because often co-workers tell
me "that can never be null". Yeah, unless, some weird condition
happens that was not tested for and surfaced after customer
installation. Better safe than sorry. Defensive programming... :-)
Regards
Henning
--
Dipl.-Inf. (Univ.) Henning P. Schmiedehausen INTERMETA GmbH
hps@intermeta.de +49 9131 50 654 0 http://www.intermeta.de/
RedHat Certified Engineer -- Jakarta Turbine Development -- hero for hire
Linux, Java, perl, Solaris -- Consulting, Training, Development
4 - 8 - 15 - 16 - 23 - 42
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: svn commit: r233505 - in /jakarta/commons/proper/configuration/trunk/src:
Posted by Brett Porter <br...@apache.org>.
How?
if keyBuffer is null (which it isn't, the constructor initialises it),
String.valueOf( keyBuffer ) would be an NPE as well. toString() should
not return null and won't for a StringBuffer.
- Brett
Henning P. Schmiedehausen wrote:
>oheger@apache.org writes:
>
>
>
>> public int hashCode()
>> {
>>- return keyBuffer.hashCode();
>>+ return keyBuffer.toString().hashCode();
>> }
>>
>>
>
>This screams "NullPointerException" all over the place.
>
>How about
>
>public int hashCode() {
> return String.valueOf(keyBuffer).hashCode();
>}
>
> Regards
> Henning
>
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org