Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > java.lang.NullPointerException

Reply
Thread Tools

java.lang.NullPointerException

 
 
John Goche
Guest
Posts: n/a
 
      02-16-2006

Hello,

I would like to point our that the following code
produces a NullPointerException at runtime:

byte[] ba = null;
String s = new String(ba);

This is somewhat odd. I thought the constructor
would simply produce an empty string. This is not
so important but I find it rather curious.
Feedback appreciated.

Regards,

JG

 
Reply With Quote
 
 
 
 
bakarirum@gmail.com
Guest
Posts: n/a
 
      02-16-2006
You created a reference to a byte[] called ba that points to null. If
you try to use that null reference in anyway you will get the Null
pointer exception. You will have to change your code to this.

byte[] ba = null;
String s;
if (ba == null) {
s = new String();
} else {
s = new String(ba);
}

 
Reply With Quote
 
 
 
 
Timo Stamm
Guest
Posts: n/a
 
      02-16-2006
John Goche schrieb:
> Hello,
>
> I would like to point our that the following code
> produces a NullPointerException at runtime:
>
> byte[] ba = null;
> String s = new String(ba);
>
> This is somewhat odd. I thought the constructor
> would simply produce an empty string. This is not
> so important but I find it rather curious.
> Feedback appreciated.



There is a difference between "empty" and "null".

This would create an empty String:

new String(new byte[]{});



Timo
 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      02-16-2006
John Goche wrote:
>
> I would like to point our that the following code
> produces a NullPointerException at runtime:
>
> byte[] ba = null;
> String s = new String(ba);
>
> This is somewhat odd. I thought the constructor
> would simply produce an empty string. This is not
> so important but I find it rather curious.


There are two obvious approaches to handling nulls: making up some
interpretation for them (so the example code above might produce a
String "null") or throwing an exception. The former tends to mask
programming errors. Although the latter stops a program working in the
case of a bug, hopefully that will lead to a correction early in
development rather than after the code has gone into production.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      02-17-2006
On 16 Feb 2006 05:49:46 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote, quoted or
indirectly quoted someone who said :

>byte[] ba = null;
>String s;
>if (ba == null) {
> s = new String();
>} else {
> s = new String(ba);
>}


i would write that more tersely as:

byte[] ba;
.....
String s = ( ba != null ) ? new String(ba) : "";

or possibly
String s = ( ba != null ) ? new String(ba) : null;
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      02-17-2006
On Fri, 17 Feb 2006 06:19:39 GMT, Roedy Green
<(E-Mail Removed) > wrote, quoted or
indirectly quoted someone who said :

>String s = ( ba != null ) ? new String(ba) : null;


As an assembler coder, I got in the habit of putting the common case
first since fall through to the true case is faster than the jump to
the else.

This is also a nice documentation aid. When reviewing code you for
algorithm understanding you can temporarily ignore the elses and odd
case exception handling.


other people might write that as:

String s = ( ba == null ) ? null : new String(ba);

because == seems more natural to them than !=
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
John Goche
Guest
Posts: n/a
 
      02-22-2006
Here's another example producing a NullPointerException at runtime:

class Foo {

public static void main(String[] args) {

String foo = "foo", bar = null;

if (foo.compareTo(bar) == 0) {

System.err.println("hello");

}


}

}

I guess checking for null strings inside the java.lang.String
compareTo(String) method is not done to reduce overhead.

Regards,

JG

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments