0

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

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • `at Test$21.windowOpened(Test.java:2545)` -- 2545? Time to refactor – Hovercraft Full Of Eels Jun 18 '17 at 00:46
  • 1
    You'll find complete examples in the Q&A'a that your question duplicates; if this is not a duplicate, please edit your question to include a [mcve] that shows your revised approach. – trashgod Jun 18 '17 at 00:49
  • 1
    This question was originally closed as a duplicate of: https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it. While that question attempts to address the cause of a "general" `ArrayIndexOutOfBoundsException:`, based on taking some time to actually look at the code, the OP doesn't understand the basics of using the DefaultTableModel and therefore more information is needed to solve the problem. So I reopened the question. – camickr Jun 18 '17 at 01:45
  • After reading @camickr's helpful answer, see the related examples [here](https://stackoverflow.com/questions/25526833/loading-and-displaying-large-text-files), showing `addRow()`, and [here](https://stackoverflow.com/questions/25526833/loading-and-displaying-large-text-files), showning a homologous `addFile()`. – trashgod Jun 18 '17 at 21:31

1 Answers1

2

First of all the point of the forum is NOT for you to dump a stack trace as though you have no idea what the Exception is trying to tell you (that is why this question was originally closed).

The first thing you do is search the forum for other questions that have an error with ArrayIndexOutOfBoundsException. Use the information found in the forum to solve (narrow down) your problem.

java.lang.ArrayIndexOutOfBoundsException: 1 >= 0

Above tells you that your index value to too large.

at javax.swing.table.DefaultTableModel.setValueAt(DefaultTableModel.java:664)

Above gives more information telling you the problem occurs when you use the setValueAt(..) method. So one of the indexes is wrong. It is your job to determine which index is wrong.

It appears to me the problem may be related to not understanding how the DefaultTableModel works internally. That is, the DefaultTableModel doesn't create the storage for you automatically when you use the setValueAt(...) method. You can only use that method if the storage for the data has already been allocated by the model.

You don't show us where you create the DefaultTableModel. So the only assumption is that you are doing it wrong.

The easiest way to solve your problem is to:

  1. Create a DefaultTableModel with just the column names and 0 rows. Read the DefaultTableModel API for the proper constructor to use.

  2. Then as you read each line of data from the file you use the addRow(...) of the DefaultTableModel to add a new row of data.

Using the suggestion from above there is no need to read the file twice. You appear to read the file once to count the number of lines (but then you never use the "lines" variable). The TableModel grows dynamically when you use the addRow(...) method

Also, in your code to save the data to a file there is no need to keep opening the file using the append parameter. The file should be opened once outside of any loop. Then your logic just add a row of data to the file. At the end of all the loops you close the file once.

camickr
  • 321,443
  • 19
  • 166
  • 288