java.lang.NullPointerException

J

John Goche

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
 
B

bakarirum

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);
}
 
T

Timo Stamm

John said:
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
 
T

Thomas Hawtin

John said:
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
 
R

Roedy Green

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;
 
R

Roedy Green

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 !=
 
J

John Goche

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top