title: Is it possible to open a file using a similar name from a database?
body: <p>So when I click a button ("Open Lesson") a JFrame from a different class will appear (using .setVisible(true)). In that JFrame there is a JTable (tblOpenLesson), and two buttons (btnOpenLesson and btnCancel).</p>
  
  <p>now what I want to happen is that when I click the Open Lesson Button it will do the following: 1. Update the table to reflect the sqlite Database (lesson) into the JTable 2. Open the selected file from the table by getting the file name (which is the same as in the database) and using it for the FileReader function.</p>
  
  <p>However, it is giving me the error: </p>
  
  <blockquote>
    <p>Exception in thread "AWT-EventQueue-0"
    java.lang.ArrayIndexOutOfBoundsException: -1  at
    java.util.Vector.elementData(Vector.java:737)     at
    java.util.Vector.elementAt(Vector.java:480)   at
    javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:648)
      at
    mainWindow.openLesson.btnOpenLessonPopUpActionPerformed(openLesson.java:135)
      at mainWindow.openLesson.access$000(openLesson.java:19)     at
    mainWindow.openLesson$1.actionPerformed(openLesson.java:60)   at
    javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
      at
    javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
      at
    javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
      at
    javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
      at
    javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
      at java.awt.Component.processMouseEvent(Component.java:6539)    at
    javax.swing.JComponent.processMouseEvent(JComponent.java:3324)    at
    java.awt.Component.processEvent(Component.java:6304)  at
    java.awt.Container.processEvent(Container.java:2239)  at
    java.awt.Component.dispatchEventImpl(Component.java:4889)     at
    java.awt.Container.dispatchEventImpl(Container.java:2297)     at
    java.awt.Component.dispatchEvent(Component.java:4711)     at
    java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
      at
    java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
      at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
      at java.awt.Container.dispatchEventImpl(Container.java:2283)    at
    java.awt.Window.dispatchEventImpl(Window.java:2746)   at
    java.awt.Component.dispatchEvent(Component.java:4711)     at
    java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)    at
    java.awt.EventQueue.access$500(EventQueue.java:97)    at
    java.awt.EventQueue$3.run(EventQueue.java:709)    at
    java.awt.EventQueue$3.run(EventQueue.java:703)    at
    java.security.AccessController.doPrivileged(Native Method)    at
    java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
      at
    java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
      at java.awt.EventQueue$4.run(EventQueue.java:733)   at
    java.awt.EventQueue$4.run(EventQueue.java:731)    at
    java.security.AccessController.doPrivileged(Native Method)    at
    java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)   at
    java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
      at
    java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
      at
    java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
      at
    java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
      at
    java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)</p>
  </blockquote>
  
  <p>I tried searching for what should I do to fix it but I'm stuck here for 3 days with no avail.</p>
  
  <p>this is the code of the popup JFrame: </p>
  
  <pre><code>@SuppressWarnings("unchecked")
  // &lt;editor-fold defaultstate="collapsed" desc="Generated Code"&gt;                          
  // &lt;/editor-fold&gt;                        
  
  private void btnCancelOpenLessonActionPerformed(java.awt.event.ActionEvent evt) {                                                    
      this.setVisible(false);
  }                                                   
  
  private void btnOpenLessonPopUpActionPerformed(java.awt.event.ActionEvent evt) {                                                   
  
      updateDBlessons(); // update list
  
      String[] fileName = null;
  
      int selectedColumn = tblOpenLesson.getSelectedColumn();
      int selectedRow = tblOpenLesson.getSelectedRow();
      try {
          Class.forName("org.sqlite.JDBC");
          Connection con = DriverManager.getConnection("jdbc:sqlite:sql_items.sqlite");
          Statement stmtGetLesson = con.createStatement();
          ResultSet rs = stmtGetLesson.executeQuery("SELECT * FROM lessons WHERE lesson_id  = '" + tblOpenLesson.getModel().getValueAt(selectedRow, selectedColumn) + "';");
  
          if (rs.next()) {
              fileName[0] = rs.getString("lesson");
          }
      } catch (SQLException e) {
          JOptionPane.showMessageDialog(null, e);
      } catch (ClassNotFoundException ex) {
          Logger.getLogger(openLesson.class.getName()).log(Level.SEVERE, null, ex);
      }
  
          try {
          FileReader fr = new FileReader("Lessons\\" + fileName[0] + ".txt");
              try (BufferedReader br = new BufferedReader(fr)) {
              mainScreen.lessonPane.read(br, null); //  lessonPane is from another class
              br.close();
              }
          } catch (IOException e) {
          System.out.println(e);
          this.setVisible(false);
          }
  
  
  }                                                  
  
  
  public static void main(String args[]) {
      /* Set the Nimbus look and feel */
      //&lt;editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "&gt;
      /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
       * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
       */
  
      //&lt;/editor-fold&gt;
  
      //&lt;/editor-fold&gt;
  
      /* Create and display the form */
      java.awt.EventQueue.invokeLater(() -&gt; {
          new openLesson().setVisible(false);
      });
  }
  
  public void updateDBlessons() {
      try {
          Class.forName("org.sqlite.JDBC");
          Connection  con = DriverManager.getConnection("jdbc:sqlite:sql_items.sqlite");
          Statement stmt = con.createStatement();
          ResultSet rs = stmt.executeQuery("SELECT * FROM lessons");
          DefaultTableModel dtm = (DefaultTableModel) tblOpenLesson.getModel();
          dtm.setRowCount(0);
  
          while(rs.next()){
              Object o[] = {rs.getString("lesson")};
              dtm.addRow(o);
          }
      } catch (ClassNotFoundException | SQLException e) {
  
      }
  }
  
  
  // Variables declaration - do not modify                     
  private javax.swing.JButton btnCancelOpenLesson;
  private javax.swing.JButton btnOpenLessonPopUp;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JPanel pnlOpenLesson;
  private javax.swing.JTable tblOpenLesson;
  // End of variables declaration                   
  </code></pre>
  
  <p>}</p>
  
  <p>I'm starting to think that it is not possible. If so, please tell me what can I do. Thank you so much</p>