-1

This is my method, I have gone through every out of bounds exception but cant work out whats going on. This is not the same as other questions as i have tried all the logical steps issued from other questions

private void gettablecount(TableModel mod){

 int r =  mod.getRowCount()+1;
 int c = mod.getColumnCount()+1;
  String[][] ps = new String[r][c];

  for (int rw = 0;rw <=r;rw++){
     for (int cl = 0;cl<=c;cl++){
       ps[rw][cl] = mod.getValueAt(rw, cl).toString();

        System.out.print(ps[rw][cl] + "  ");

     }
     System.out.println();
     } 
  }

my table model mod has r = 133 rows and c = 249 columns, I then try and put this into a 2d array, and no matter what i try i keep getting out of bounds exceptions, please can someone provide some help? The errors i have got is x>=x or -1, or just x, in trying to work this out. Should be quite straight forward but i dont know whats going on.

I have just tried this:

private void gettablecount(TableModel mod){

 int r =  mod.getRowCount()+1;
 int c = mod.getColumnCount()+1;
String[][] ps = new String[r][c];

for (int rw = 0;rw <r;rw++){
    for (int cl = 0;cl<c;cl++){
        ps[rw][cl] = mod.getValueAt(rw, cl).toString();

        System.out.print(ps[rw][cl] + "  ");

    }
    System.out.println();
} 
  }

But i get the stacktrace:

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

Here is my vector where line 474 is, i have annotated line 474

public synchronized E elementAt(int index) {
    if (index >= elementCount) {
//line 474 below
        throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
    }

    return elementData(index);
}
Ingram
  • 654
  • 2
  • 7
  • 29

1 Answers1

0
String[][] ps = new String[r][c];

for (int rw = 0;rw <=r;rw++){
    for (int cl = 0;cl<=c;cl++){
        ps[rw][cl] = mod.getValueAt(rw, cl).toString();
        //System.out.println(powerstation[rw][cl] + "\t");
        System.out.print(ps[rw][cl] + "  ");

    }
    System.out.println();
} 

ps has the length of r, but the last index is r-1 sinces array-indices start at 0. The last iteration of your for-loops throw the exception since ps[r][c] doesn't exist.

You need to change <= r and <= c to < r and < c.

RaminS
  • 2,208
  • 4
  • 22
  • 30
  • I have updated my question on what i did, it didnt work – Ingram Mar 27 '16 at 19:58
  • @MrAssistance You have an error on line 474. I am not seeing what you do on line 474. Apparently you have a Vector and you are doing the same mistake there as you did in `gettablecount`. – RaminS Mar 27 '16 at 20:07
  • i will post up the method where row 474 is and annotate, basically what this method does is fill in the gaps in my table model. My table model has lots of blanks in its raw form so i take the data in a cell that is to the left, whether that be 1 cell or 5 cells to the left – Ingram Mar 27 '16 at 20:10
  • @ Gendarme posted question is updated – Ingram Mar 27 '16 at 20:12
  • @ Gendarme I dont quite understand why this question is proving so difficult for me because i already create a Jtable with all the values in a 2 d array. All i want to do is get the JTable data into a 2d array for further processing.. – Ingram Mar 27 '16 at 21:07
  • @MrAssistance What you have posted is not line 474, since there is no Vector at sight. – RaminS Mar 27 '16 at 21:08
  • @ Gendarme apologies i thought you meant of the main code - please see my question for the revision – Ingram Mar 27 '16 at 21:13
  • Where in your code do you use `elementAt`? – RaminS Mar 27 '16 at 21:18
  • @ Gendarme I will have a look – Ingram Mar 27 '16 at 21:24