Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

java.lang.NullPointerException

 
 
NickPick
Guest
Posts: n/a
 
      03-02-2009
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[i]=Float.parseFloat(price[4]);
} while (line != null);
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      03-02-2009
NickPick <> writes:
> String[] price = new String[1000];
> line = buff.readLine();


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

> price = line.toString().split (","); // CAUSING
>NullPointerException.
> i++;
> p[i]=Float.parseFloat(price[4]);
> } while (line != null);

 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      03-02-2009
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[i]=Float.parseFloat(price[4]);


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

> } while (line != null);



--
Lew
 
Reply With Quote
 
Joshua Cranmer
Guest
Posts: n/a
 
      03-02-2009
NickPick wrote:
> do {
> String[] price = new String[1000];
> line = buff.readLine();
> price = line.toString().split (","); // CAUSING
> NullPointerException.
> i++;
> p[i]=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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-02-2009
On 2 Mar 2009 15:09:45 GMT, (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))
 
Reply With Quote
 
wim
Guest
Posts: n/a
 
      03-03-2009
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[i]=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
}
 
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
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57