Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   java.lang.NullPointerException (http://www.velocityreviews.com/forums/t673928-java-lang-nullpointerexception.html)

NickPick 03-02-2009 02:45 PM

java.lang.NullPointerException
 
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);

Stefan Ram 03-02-2009 03:09 PM

Re: java.lang.NullPointerException
 
NickPick <dickreuter@yahoo.com> 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);


Lew 03-02-2009 03:20 PM

Re: java.lang.NullPointerException
 
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

Joshua Cranmer 03-02-2009 03:44 PM

Re: java.lang.NullPointerException
 
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

Roedy Green 03-02-2009 11:12 PM

Re: java.lang.NullPointerException
 
On 2 Mar 2009 15:09:45 GMT, ram@zedat.fu-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))

wim 03-03-2009 08:36 PM

Re: java.lang.NullPointerException
 
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
}


All times are GMT. The time now is 08:21 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.