Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

java.lang.NullPointerException

Thread view: 
John Goche - 16 Feb 2006 13:44 GMT
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
bakarirum@gmail.com - 16 Feb 2006 13:49 GMT
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);
}
Roedy Green - 17 Feb 2006 06:19 GMT
>byte[] ba = null;
>String s;
[quoted text clipped - 3 lines]
>    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;
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 17 Feb 2006 06:43 GMT
On Fri, 17 Feb 2006 06:19:39 GMT, Roedy Green
<my_email_is_posted_on_my_website@munged.invalid> 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 !=
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Timo Stamm - 16 Feb 2006 14:03 GMT
John Goche schrieb:
> Hello,
>
[quoted text clipped - 8 lines]
> 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
Thomas Hawtin - 16 Feb 2006 14:41 GMT
> I would like to point our that the following code
> produces a NullPointerException at runtime:
[quoted text clipped - 5 lines]
> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

John Goche - 22 Feb 2006 17:53 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.