public boolean ReadFile(){
File file = new File("cd.dat");
BufferedReader bufferedReader;
String line= "";
try{
bufferedReader = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException nf){
return false;
}catch (NullPointerException ee){
return false;
}
i = 0;
try
{
while((line = bufferedReader.readLine()) != null) {
//accountnumbers[i] = line;
i = i + 1;
System.out.println(line);
}
bufferedReader.close();
}
catch (IOException e){
return false;
}
catch (NullPointerException efef){
return false;
}
return true;
}
Everytime the program hits the last line in the file, it gives the error NullPointerException. Doesn't the while condition test for this? how else can I check for EOF?
Thanks.
-
ARGH!! What's wrong with this? (1 messages)
- Posted by: Julie Woo
- Posted on: August 13 2001 15:23 EDT
Threaded Messages (1)
- ARGH!! What's wrong with this? by Andy Nguyen on August 14 2001 10:34 EDT
-
ARGH!! What's wrong with this?[ Go to top ]
- Posted by: Andy Nguyen
- Posted on: August 14 2001 10:34 EDT
- in response to Julie Woo
I don't think the NullPointerException is coming from the line that was read. It might be coming from the close() method. Try this for your read loop:
try {
while ((line = bufferedReader.readLine()) != null) {
i = i + 1;
System.out.println(line);
}
}
catch (IOException e) {
return false;
}
catch (NullPointerException efef) {
return false;
}
finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
}
catch (Exception ignore) {
}
}
}
return true;
Hope that helps,
Andy