private void getProductsFromFile() { //---now open the file, read each line, and add that line to our JList model String lineFromFile = null; BufferedReader myReader = null; //declare here so you can use it in try-catch-finally blocks try { FileReader fr = new FileReader("products.txt"); //data file must be in root of project folder! myReader = new BufferedReader (fr); //instantiate it inside the try block lineFromFile= myReader.readLine(); //read first line and add to model model.addElement(lineFromFile); while (lineFromFile != null) { lineFromFile= myReader.readLine(); //read the rest of the file if (lineFromFile != null) { model.addElement(lineFromFile); } } } catch (IOException e) { String message = "An error occurred while reading the products file. Do you want to exit?"; String title = "File Reading Error"; // display the JOptionPane showConfirmDialog int reply = JOptionPane.showConfirmDialog(this, message + " \n" + e.getMessage().toString(), title, JOptionPane.YES_NO_OPTION); if (reply == JOptionPane.YES_OPTION) { System.exit(0); } } finally { try { myReader.close(); //this executes whether there is an error or not! But must be inside it's own try-catch } catch (IOException ex) { } } }