java.lang.NullPointerException

N

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);
 
S

Stefan Ram

NickPick said:
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);
 
L

Lew

NickPick said:
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.
 
J

Joshua Cranmer

NickPick said:
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.
 
R

Roedy Green

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))
 
W

wim

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
}
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top