java.lang.NullPointerException

Discussion in 'Java' started by John Goche, Feb 16, 2006.

  1. John Goche

    John Goche Guest

    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
     
    John Goche, Feb 16, 2006
    #1
    1. Advertising

  2. John Goche

    Guest

    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);
    }
     
    , Feb 16, 2006
    #2
    1. Advertising

  3. John Goche

    Timo Stamm Guest

    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
     
    Timo Stamm, Feb 16, 2006
    #3
  4. 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/
     
    Thomas Hawtin, Feb 16, 2006
    #4
  5. John Goche

    Roedy Green Guest

    On 16 Feb 2006 05:49:46 -0800, 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.
     
    Roedy Green, Feb 17, 2006
    #5
  6. John Goche

    Roedy Green Guest

    On Fri, 17 Feb 2006 06:19:39 GMT, Roedy Green
    <> 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.
     
    Roedy Green, Feb 17, 2006
    #6
  7. John Goche

    John Goche Guest

    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
     
    John Goche, Feb 22, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dhek Bhun Kho
    Replies:
    0
    Views:
    2,257
    Dhek Bhun Kho
    Jul 9, 2003
  2. Victoria
    Replies:
    4
    Views:
    661
  3. Mr Smith
    Replies:
    7
    Views:
    13,723
    Mr Smith
    Feb 21, 2005
  4. Fahad
    Replies:
    1
    Views:
    613
    Andrew Thompson
    Aug 8, 2005
  5. Replies:
    7
    Views:
    7,611
    Anonymous
    Oct 14, 2005
Loading...

Share This Page