exception in fiel

hi ,

am getting a null pointer exception in the code,

actually i have a large set of data in my file as

18.0909 1

19.0872 2

....

i am reading the file and tryin to extract the data in two arrays as float and integer,

am getting exception

caan you help me to solve that ....

try{

FileReader fread =new FileReader("DIS.txt");

BufferedReader bread =new BufferedReader(fread);

int i=0;

int k=0;

do{

text = bread.readLine();

value = text.split("\\s");// null pointer exception @ this line

fvalue[i] = Float.parseFloat(value[0]);

System.out.println("float value :"+fvalue[i]);

ivalue[k] = Integer.parseInt(value[1]);

System.out.println("int value :"+ivalue[k]);

i++;

k++;

}while(text !=null);

[1308 byte] By [prasadgunasa] at [2007-11-27 8:32:43]
# 1
hmmm... what makes you think text is not null?
corlettka at 2007-7-12 20:28:38 > top of Java-index,Java Essentials,Java Programming...
# 2
am not sure ,the compiler says that null pointer exception i s@ that line
prasadgunasa at 2007-7-12 20:28:38 > top of Java-index,Java Essentials,Java Programming...
# 3
yeah so text is null then isn't it... which means there's a blank line in your file... probably at the end of the file.
corlettka at 2007-7-12 20:28:38 > top of Java-index,Java Essentials,Java Programming...
# 4

> am getting a null pointer exception in the code

This is because text , thatis the result of bread.readLine();, is null.

You must test if text is null immediately after the bread.readLine() or anyway before text is used, and if null, exit the do loop. Some rewriting of the code might do good.

Message was edited by: java_knight

java_knighta at 2007-7-12 20:28:38 > top of Java-index,Java Essentials,Java Programming...
# 5
yes ,there was a blank line i deleted it and saved it but still showing the same exception.
prasadgunasa at 2007-7-12 20:28:38 > top of Java-index,Java Essentials,Java Programming...
# 6
if (text==null) break;
corlettka at 2007-7-12 20:28:38 > top of Java-index,Java Essentials,Java Programming...
# 7
> yes ,there was a blank line i deleted it and saved it but still showing the same exception.This is because readLine() is arrived at the end of the file.Message was edited by: java_knight
java_knighta at 2007-7-12 20:28:38 > top of Java-index,Java Essentials,Java Programming...
# 8
sorry ,do i want to add it afetr the while loop or where do i want to add thisthank you,
prasadgunasa at 2007-7-12 20:28:38 > top of Java-index,Java Essentials,Java Programming...
# 9
The rest is easy... you'll figure it out if you try... stop posting and start programming again ;-) Keith.
corlettka at 2007-7-12 20:28:38 > top of Java-index,Java Essentials,Java Programming...
# 10
because i added that and tried, but still showing the exception,is my code right as follows,do{if(text == null)break;}while(text!=null);
prasadgunasa at 2007-7-12 20:28:38 > top of Java-index,Java Essentials,Java Programming...
# 11
sorry for repeated posts,i ll try and sort it out,thanks for your guidance
prasadgunasa at 2007-7-12 20:28:38 > top of Java-index,Java Essentials,Java Programming...
# 12
are you using java 1.6 or an earlier version... 1.6 has easier ways to solve this problem.No, I'm not telling you where to put that line on code... I'm not you're wet nurse.Keith.
corlettka at 2007-7-12 20:28:38 > top of Java-index,Java Essentials,Java Programming...
# 13
am using version 1.4 i think,yeah its netbeans 4.1,
prasadgunasa at 2007-7-12 20:28:38 > top of Java-index,Java Essentials,Java Programming...
# 14
Ok, just keep trying... 1.6 has a Scanner which hides the details of the string to numeric conversion for you.Also, I think it might be a good idea to create a Row class, which just contains a float and an int... and have one array of Rows.
corlettka at 2007-7-12 20:28:38 > top of Java-index,Java Essentials,Java Programming...