Question: I'm using JUnit test intellij to test my code and I'm trying to debug an arrayOutOfBounds exception. This is the test: @Test void getElem() {
I'm using JUnit test intellij to test my code and I'm trying to debug an arrayOutOfBounds exception.
This is the test:
@Test void getElem() { Matrix m = new Matrix(new Scanner(simpleMatrix)); assertEquals(2, m.getElem(1,2), "getElem() did not return correct value"); } This is getElem method:
public double getElem(int i, int j) { // return corresponding entry in matrix. return matrix[i][j]; } This is the error I get when I try to run the getElem test:
java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
This is a matrix class for loading and modifying matrices. I'm quite lost on where the error is, as the only place it could be is in the getElem() according to the debugger. Can someone take a look?
Step by Step Solution
3.46 Rating (153 Votes )
There are 3 Steps involved in it
1 Check the values of i and j in your test case In your test case you are calling the getElem method ... View full answer
Get step-by-step solutions from verified subject matter experts
