How to import a 2D Array into a JTable from a CSV file

147 views Asked by At

I am trying to implement a text file database system through a CSV file into a Java GUI, I have created the GUI, the database, the methods required to import and export to the file, and the method to convert the information into a 2DArray from the CSV file. I am trying to make a simple password vault but whenever I try adding something to the file I get an error stating:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at TextFileDatabase.readDatabase(TextFileDatabase.java:93)
    at PasswordVault.<init>(PasswordVault.java:97)
    at PasswordVault.main(PasswordVault.java:22)

The method that is returning the error is the readDatabase() one and it is as follows:

public static String[][] readDatabase()
    {
        try
        {
            kb = new Scanner(database);
        }
        catch(FileNotFoundException e)
        {
            e.printStackTrace();
        }

        //since the size of the database is unknown, we'll first work in a temporary 2D arraylist that can dynamically change its size
        ArrayList<String[]> tempDatabase = new ArrayList<String[]>();

        while(kb.hasNext()) 
        {
            String tempLine = kb.nextLine(); //reads the first/next line of the database
            String[] tempData = tempLine.split(","); //gets that result as a string, splits it up into an array based on commas
            tempDatabase.add(tempData); //adds the string array to the arraylist
        }

        //at this point, the while loop should have traversed through the entire file

        String[][] output = new String[tempDatabase.size()][3];
            //this is the string we'll return
            //the column size is the size of the tempDatabase arraylist because thats the number of passwords we have stored
            //the row size is three because each row is an 1) app, 2) password, 3) description 

        //converts the ArrayList to the actual output array

        for(int x=0; x<output.length; x++)
        {
            for(int y=0; y<3; y++)
            {
                //This is line 93 in TextFileDatabase()
                output[x][y] = tempDatabase.get(x)[y];
            }
        }

        return output;
    }

and it is pointing to this section of code in my PasswordVault() class

        //This is line 97
        String[][] data = TextFileDatabase.readDatabase();

        //This will convert the information from the 2DArray made from the file into a table model to use in the GUI 
        for(int a = 0; a < data.length; a++)
        {
            String[] row = new String[data[a].length];

            for(int b = 0; b < data[a].length; b++)
            {
                row[b] = data[a][b];
            }

            tableModel.addRow(row);
        }

And the last line the error calls to is just the line calling the PasswordVault() constructor

new PasswordVault();
1

There are 1 answers

1
Adao Amaral On

The problem is here:

output[x][y] = tempDatabase.get(x)[y];

TempDatabase try to access a inexistent location. You have 3 elements em ALL strings[] inside tempdatabase? Check it.