Evan
Evan

Reputation: 13

Array Index Out Of Bounds Exception in JTable

    public int getSum(){
        int sum=0;

        for(int i=1; i <= sqlite_master.getRowCount(); i++)
        {
            sum=sum+Integer.parseInt((String) sqlite_master.getValueAt(i, 2));      
        }
        return sum;
    }

The code is written in order to get the sum of a particular column in a table of 2 columns with one column strings and other numbers

Error is

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
at java.util.Vector.elementAt(Vector.java:474)

kindly provide some solution in order to fix the problem

Upvotes: 1

Views: 2368

Answers (1)

TT.
TT.

Reputation: 16137

This should work better. Indexes start at 0 and go up to row count minus 1.

public int getSum(){
        int sum=0;

        for(int i=0; i < sqlite_master.getRowCount(); i++)
        {
            sum=sum+Integer.parseInt((String) sqlite_master.getValueAt(i, 2));      
        }
        return sum;
    }

Upvotes: 2

Related Questions