Discussions

Web tier: servlets, JSP, Web frameworks: ARGH!! What's wrong with this?

  1. ARGH!! What's wrong with this? (1 messages)

    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.
  2. ARGH!! What's wrong with this?[ Go to top ]

    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