Mon code est trop grand pour l'attacher ici. Je vais vous fournir juste la partie avec le problème.Charger les données JTable à partir de txt
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();
}
}
});
Cette partie tente de charger les données enregistrées à partir du fichier txt. Je vais aussi vous donner la partie qui enregistre ces informations dans le fichier txt:
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();
}
}
}
}
}
});
Les données que nous recevons de la JTable sont comme ceux-ci:
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~---
Les données sont enregistrées. Et maintenant je fais face à un problème en essayant de les charger! Voici l'exception que je reçois:
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)
Certaines parties du code sont en grec, désolé
'Au test $ 21.windowOpened (Test.java:2545)' - 2545? Il est temps de refactoriser –
Vous trouverez des exemples complets dans le Q & A'a que votre question duplique; S'il ne s'agit pas d'un doublon, veuillez modifier votre question pour inclure un [mcve] qui montre votre approche révisée. – trashgod
Cette question a été fermée à l'origine comme un double de: https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it. Tandis que cette question tente d'adresser la cause d'une "ArrayIndexOutOfBoundsException": "générale", en prenant le temps de regarder le code, l'OP ne comprend pas les bases de l'utilisation de DefaultTableModel et donc plus d'informations sont nécessaires pour résoudre le problème. J'ai donc rouvert la question. – camickr