My code is too big to attach it here. I will provide you just the part with the issue.
frame2.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
File table_f = new File("C:\\Database\\Table\\Table.txt");
String table_f_path = table_f.getPath();
try {
BufferedReader b_reader = new BufferedReader(new FileReader(new File(table_f_path)));
BufferedReader reader = new BufferedReader(new FileReader(table_f_path));
int lines = 0;
while (reader.readLine() != null) lines++;
reader.close();
String table_apo_out;
for (int y = 1;y < lines;y++){
table_apo_out = b_reader.readLine();
String[] table_apo_out_array = table_apo_out.split("~");
for (int z=0;z < 20;z++) {
System.out.println(Arrays.toString(table_apo_out_array));
System.out.println(y);
System.out.println(lines);
model_table.setValueAt(table_apo_out_array[z],y,z);
}
}
} catch (IOException qq) {
qq.printStackTrace();
}
}
});
This part tries to load the saved data from the txt file. I will also give you the part that saves these info in the txt file:
frame2.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
File table_f = new File("C:\\Database\\Table\\Table.txt");
int num_row = table.getRowCount();
int num_columns = 21;
for (int n = 0;n < num_row; n++){
for(int m = 0;m < num_columns; m++){
FileWriter f_writer = null;
BufferedWriter b_writer = null;
String table_f_path = table_f.getPath();
try {
f_writer = new FileWriter(table_f_path, true);
b_writer = new BufferedWriter(f_writer);
if (m == 20) {
Object com = model_table.getValueAt(n, m);
String com_str = com.toString();
b_writer.write(com_str+"\r\n");
} else if (m == 0){
continue;
} else {
Object data = model_table.getValueAt(n, m);
String data_str = data.toString();
b_writer.write(data_str+"~");
}
} catch (IOException | NullPointerException | ArrayIndexOutOfBoundsException io){
io.printStackTrace();
} finally {
try {
if (b_writer != null)
b_writer.close();
if (f_writer != null)
f_writer.close();
if(n == num_row){
frame2.dispose();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
}
});
The data we get from the JTable are like these:
300/50~Δευτέρα 12 Μαΐου 2014~gak~gak~gak~gak~gak~gak~gak~gak~gak~gak~gak~Παρασκευή 5 Μαΐου 2017~524.0~2452.0~gak~2452.0~gak~245245
300/50~Δευτέρα 12 Μαΐου 2014~gak~gak~gak~gak~gak~gak~gak~gak~gak~gak~gak~Παρασκευή 5 Μαΐου 2017~524.0~2452.0~gak~2452.0~gak~---
The data are saved. And now I face a problem trying to load them! Here is the Exception I get:
Exception in thread "AWT-EventQueue-0"
java.lang.ArrayIndexOutOfBoundsException: 1 >= 0
at java.util.Vector.elementAt(Vector.java:474)
at javax.swing.table.DefaultTableModel.setValueAt(DefaultTableModel.java:664)
at Test$21.windowOpened(Test.java:2545)
at java.awt.AWTEventMulticaster.windowOpened(AWTEventMulticaster.java:340)
at java.awt.Window.processWindowEvent(Window.java:2051)
at javax.swing.JFrame.processWindowEvent(JFrame.java:305)
at java.awt.Window.processEvent(Window.java:2013)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
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:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
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)
Some parts of the code are in greek, sorry