Итак, у меня есть метод, который (должен) возвращать строку, содержащую данные из базы данных sqlite:
public String getLesson() throws ClassNotFoundException {
ArrayList list = new ArrayList();
int selectedColumn = tblOpenLesson.getSelectedColumn();
int selectedRow = tblOpenLesson.getSelectedRow();
String name = (String) tblOpenLesson.getModel().getValueAt(selectedRow, selectedColumn);
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 = '" + name + "';");
if (rs.next()) {
list.add(rs.getString("lesson"));
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e);
} catch (ClassNotFoundException ex) {
Logger.getLogger(openLesson.class.getName()).log(Level.SEVERE, null, ex);
}
return (String) list.get(0);
}
, и я бы использовал эту строку, чтобы найти текстовый файл, по которому его содержимое будет отображается на JTextPane, расположенном в другом классе
private void btnOpenLessonPopUpActionPerformed(java.awt.event.ActionEvent evt) {
try {
FileReader fr = new FileReader("Lessons\\" + getLesson() + ".txt"); //this is Line 126
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);
} catch (ClassNotFoundException ex) {
Logger.getLogger(openLesson.class.getName()).log(Level.SEVERE, null, ex);
}
}
, однако я получаю эту ошибку:
Исключение в потоке "AWT-EventQueue-0" java .lang .IndexOutOfBoundsException: индекс: 0, размер: 0 в java .util.ArrayList.rangeCheck (ArrayList. java: 657) в java .util.ArrayList.get (ArrayList. java: 433) в главном окне .openLesson.getLesson (openLesson. java: 204) в mainWindow.openLesson.btnOpenLessonPopUpActionPerformed (openLesson. java: 126) в mainWindow.openLesson.access $ 000 (openLesson. java: 19 в) в 1910) в 19). actionPerformed (openLesson. java: 60) в javax.swing.AbstractButton.fireActionPerformed (AbstractButton. java: 2022) в javax.swing.AbstractButton $ Handler.actionPerformed (AbstractButton. java: 2348sw) в javax. .DefaultButtonModel.fireActionP erformed (DefaultButtonModel. java: 402) в javax.swing.DefaultButtonModel.setPressed (DefaultButtonModel. java: 259) в javax.swing.plaf.basi c .BasicButtonListener.mouseReistened (252). ) в java .awt.Component.processMouseEvent (Component. java: 6539) в javax.swing.JComponent.processMouseEvent (JComponent. java: 3324) в java .awt.Component.processEvent (Component. java: 6304) в java .awt.Container.processEvent (Container. java: 2239) в java .awt.Component.dispatchEventImpl (Component. java: 4889) в java .awt .Container.dispatchEventImpl (Container. java: 2297) в java .awt.Component.dispatchEvent (Component. java: 4711) в java .awt.LightweightDispatcher.retargetMouseEvent (Container. java: 4904 ) в java .awt.LightweightDispatcher.processMouseEvent (Container. java: 4535) в java .awt.LightweightDispatcher.dispatchEvent (Container. java: 4476) в java .awt.Container.dispatchEventImpl ( Контейнер. java: 2283) в java .awt.Window.dispatchEventImpl (Window. * 105 1 *: 2746) в java .awt.Component.dispatchEvent (Component. java: 4711) в java .awt.EventQueue.dispatchEventImpl (EventQueue. java: 760) в java .awt. EventQueue.access $ 500 (EventQueue. java: 97) в java .awt.EventQueue $ 3.run (EventQueue. java: 709) в java .awt.EventQueue $ 3.run (EventQueue. java) : 703) в java .security.AccessController.doPrivileged (собственный метод) в java .security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege (ProtectionDomain. java: 74) в java .security.ProtectionDomainImplIctionPotionInter_Direct_Direct_Direct_DirectoryDirect_Ware JavaSec (ProtectionDomain. java: 84) в java .awt.EventQueue $ 4.run (EventQueue. java: 733) в java .awt.EventQueue $ 4.run (EventQueue. java: 731) в java .security.AccessController.doPrivileged (собственный метод) в java .security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege (ProtectionDomain. java: 74) в java .awt.EventQueue.dispatchEvent. Событие (10). : 730) в java .awt.EventDispatchThread.pumpOneEventForFilters (EventDispa tchThread. java: 205) в java .awt.EventDispatchThread.pumpEventsForFilter (EventDispatchThread. java: 116) в java .awt.EventDispatchThread.pumpEventsForHierarchy (EventDispatchThread. java) 105: at .awt.EventDispatchThread.pumpEvents (EventDispatchThread. java: 101) в java .awt.EventDispatchThread.pumpEvents (EventDispatchThread. java: 93) в java .awt.EventDispatchThread.run (Event.D). : 82)
Я не думаю, что имею какое-либо представление о том, почему это происходит. Я все еще новичок в программировании.