0
public class Matrix
{


  private int[][] matrix;
  private int rows;
  private int cols;

  public Matrix(int r, int c)
  {
    matrix = new int[r][c];
    this.rows = r;
    this.cols = c;
  }
  public Matrix(int[][] m)
  {
    matrix = new int[m.length][m[0].length];
    this.rows = m.length;
    this.cols = m[0].length;

    for(int i=0; i<m.length; i++)
    {
      for(int j=0; j<m[0].length; j++)
      {
        matrix[i][j] = m[i][j];
      }
    }
  }

This is how I started my class with my constructors and later on I included the code:

public int get(int r, int c)
  {
    return matrix[r][c];
  }

Can anyone please explain to me why I am getting the ArrayIndexOutOfBoundsException here? This was my error:

 java.lang.ArrayIndexOutOfBoundsException: 0
    at Matrix.get(Matrix.java:117)
    at MatrixTest.dispatch(MatrixTest.java:76)
    at MatrixTest.main(MatrixTest.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

I made a tester class and called the get method like this:

case 5:
        System.out.println(matrix.get(0, 0));
        break;

After this:

       int[][] n = new int[r][c];
         for(int i=0; i<n.length; i++)
         {
           for(int j=0; j<n[0].length; j++)
           {
             n[i][j] = scan.nextInt();
           }
         }

       Matrix matrix = new Matrix(n);
       break;
  • 1
    *Where* are you getting the exception? Can you show us the stack trace? – user2357112 Dec 18 '13 at 03:47
  • Can you show the code that calls the get method and how you made that Matrix instance? It seems like you're probably just using a number thats bigger than the size of the array – Alex Coleman Dec 18 '13 at 03:47
  • Either `r` or `c` steps off of your array. We don't know which (it could be both), but I don't see any harm in checking the boundaries. – Makoto Dec 18 '13 at 03:47
  • 2
    The stack trace says that the out-of-bounds index is 0, so it looks like you've created a zero-length array somewhere. Unfortunately, the line 117 is also an out-of-bounds index since you've only posted 28 lines of code in Matrix.java :) Possibly in the last piece of code, where you say `new int[r][c]`, either `r` or `c` is 0? – ajb Dec 18 '13 at 04:06
  • 1
    @ajb I would post that as an answer (I was going to but then I saw your comment so I'll let you do it). – Jim Garrison Dec 18 '13 at 04:42

2 Answers2

0

There is very little to go on here but are you taking into account the fact that if you declare a new int[3] that the first position in the array is [0] and the last is [2] ?

Why don't you output the values of r and c right before the line that throws the exception?

user1445967
  • 1,520
  • 4
  • 14
  • 30
0

The stack trace says that the out-of-bounds index causing the exception is 0, so this means you've created a zero-length array somewhere. I can't tell where from the code you posted, but probably in this statement:

   int[][] n = new int[r][c];

either r or c is 0.

ajb
  • 31,309
  • 3
  • 58
  • 84