java.lang.NullPointerException

Discussion in 'Java' started by NickPick, Mar 2, 2009.

  1. NickPick

    NickPick Guest

    I have a problem with the .split method. It causes a
    NullPointerException when I try to read form a .csv file which has
    several lines of comma separated values which I'd like to put into p
    []. The code works fine when I remove the do..while. Do I maybe have
    to re-initialise the String[] price somehow? Any advice is
    appreciated.

    do {
    String[] price = new String[1000];
    line = buff.readLine();
    price = line.toString().split (","); // CAUSING
    NullPointerException.
    i++;
    p=Float.parseFloat(price[4]);
    } while (line != null);
    NickPick, Mar 2, 2009
    #1
    1. Advertising

  2. NickPick

    Stefan Ram Guest

    NickPick <> writes:
    > String[] price = new String[1000];
    > line = buff.readLine();


    java.lang.System.err.println( line );

    > price = line.toString().split (","); // CAUSING
    >NullPointerException.
    > i++;
    > p=Float.parseFloat(price[4]);
    > } while (line != null);
    Stefan Ram, Mar 2, 2009
    #2
    1. Advertising

  3. NickPick

    Lew Guest

    NickPick wrote:
    > I have a problem with the .split method. It causes a
    > NullPointerException when I try to read form a .csv file which has
    > several lines of comma separated values which I'd like to put into p
    > []. The code works fine when I remove the do..while. Do I maybe have
    > to re-initialise the String[] price somehow?


    No,

    >
    > do {
    > String[] price = new String[1000];


    This initialization is a complete waste, as you throw away the String[1000].

    > line = buff.readLine();
    > price = line.toString().split (","); // CAUSING
    > NullPointerException.


    'line' is 'null'.

    > i++;
    > p=Float.parseFloat(price[4]);


    You will have an exception if there are fewer than five Strings in the array.

    > } while (line != null);



    --
    Lew
    Lew, Mar 2, 2009
    #3
  4. NickPick wrote:
    > do {
    > String[] price = new String[1000];
    > line = buff.readLine();
    > price = line.toString().split (","); // CAUSING
    > NullPointerException.
    > i++;
    > p=Float.parseFloat(price[4]);
    > } while (line != null);


    This would be the programming equivalent of "Closing the barn door after
    the horse has gone." Ignoring a useless initialization, a magic array
    access without checking, Usenet formatting errors, and a minor stylistic
    complaint, your problem is that you're checking for line being null
    after you use it.


    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
    Joshua Cranmer, Mar 2, 2009
    #4
  5. NickPick

    Roedy Green Guest

    On 2 Mar 2009 15:09:45 GMT, -berlin.de (Stefan Ram) wrote,
    quoted or indirectly quoted someone who said :

    >> line = buff.readLine();

    >
    > java.lang.System.err.println( line );


    a null line is how Java tells you about EOF, not an EOFException.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    "Learning is not compulsory... neither is survival."
    ~ Dr. W. (William) Edwards Deming (born: 1900-10-14 died: 1993-12-20 at age: 93))
    Roedy Green, Mar 2, 2009
    #5
  6. NickPick

    wim Guest

    Op 02-03-09 15:45, schreef NickPick:
    > I have a problem with the .split method. It causes a
    > NullPointerException when I try to read form a .csv file which has
    > several lines of comma separated values which I'd like to put into p
    > []. The code works fine when I remove the do..while. Do I maybe have
    > to re-initialise the String[] price somehow? Any advice is
    > appreciated.
    >
    > do {
    > String[] price = new String[1000];
    > line = buff.readLine();
    > price = line.toString().split (","); // CAUSING
    > NullPointerException.
    > i++;
    > p=Float.parseFloat(price[4]);
    > } while (line != null);


    while (null != (line = buff.readLine())) {
    String[] price = new String[1000];
    //anything you like
    //line will never be nill
    }
    wim, Mar 3, 2009
    #6
    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,224
    Dhek Bhun Kho
    Jul 9, 2003
  2. Victoria
    Replies:
    4
    Views:
    632
  3. Mr Smith
    Replies:
    7
    Views:
    13,530
    Mr Smith
    Feb 21, 2005
  4. Fahad
    Replies:
    1
    Views:
    575
    Andrew Thompson
    Aug 8, 2005
  5. Replies:
    7
    Views:
    7,516
    Anonymous
    Oct 14, 2005
Loading...

Share This Page