-1

I'm working on Java via the Codefights website. This is the exercise:

After becoming famous, CodeBots decided to move to a new building and live together. The building is represented by a rectangular matrix of rooms, each cell containing an integer - the price of the room. Some rooms are free (their cost is 0), but that's probably because they are haunted, so all the bots are afraid of them. That is why any room that is free or is located anywhere below a free room in the same column is not considered suitable for the bots. Help the bots calculate the total price of all the rooms that are suitable for them. Example: For

matrix = [[0, 1, 1, 2], 
          [0, 5, 0, 0], 
          [2, 0, 3, 3]]

the output should be matrixElementsSum(matrix) = 9. Here's the rooms matrix with unsuitable rooms marked with 'x':

[[x, 1, 1, 2],
 [x, 5, x, x],
 [x, x, x, x]]

Thus, the answer is 1 + 5 + 1 + 2 = 9.

I understand that I've got to go through all the elements of the array, which is easy. The part I'm hung up on is checking on the "0" rooms above the current element in the array. I know that that position will be matrix[i-1][j], where the current position in the array is matrix[i][j]. But, I'm getting an error "InvocationTargetExeption" when I try to implement this.

My assumption is that I'm asking for negative numbers in the array address, but I'm not sure how to fix this. Here's my code:

int matrixElementsSum(int[][] matrix) {
    int sum = 0;
    for(int i = 0; i < matrix.length; i++){
        for(int j = 0; j < matrix[i].length; j++){
            if(matrix[i-1][j]==0){ sum += 0;}   
            else{sum += matrix[i][j];}
                }
        }
    return sum;
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Saint Razz
  • 53
  • 1
  • 7
  • 2
    It could still be an AIOOBE. You need to check the rest of the stacktrace (and publish it here). – Hovercraft Full Of Eels Sep 05 '17 at 16:35
  • 1
    You have i start at 0, and you use i-1 in your if statement, so when i = 0, you are calling matrix[-1] – Tyler Sep 05 '17 at 16:36
  • With i==0 you try to access `matrix[-1]` in the if-statement leading to the exception. How do you invoke this code? It seems there is some framework around this. The exception should continue with "Caused by" and sooner than later there should be an ArrayIndexOutOfBoundsException or something similar – Lothar Sep 05 '17 at 16:37

1 Answers1

0

It is an ArrayIndexOutOfBoundsException and you can fix it by replacing:

if(matrix[i-1][j]==0){ sum += 0;}

with:

if(i > 0 && matrix[i-1][j]==0){ sum += 0;}

You're getting it because you're trying to access: matrix[i-1][j] and when i=0 then i-1 = -1...

Running the fixed code on the matrix in the example you provided returned 9 as expected.

The only reason I can think of, that you're seeing an InvocationTargetException is if you invoke matrixElementsSum using reflection: then the method throws an ArrayIndexOutOfBoundsException which is wrapped by InvocationTargetException.

From Method.java docstring:

* @exception InvocationTargetException if the underlying method
*              throws an exception.
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • Oh, nice! The and statement means that if i is negative, the matrix isn't even referenced if the address would be negative, so no exception. I appreciate the help, but now what do we do about the fact that we also have to assess the room above the room above. That would mean the same exception would be thrown if i were merely 1, since 1 - 2 is still negative. – Saint Razz Sep 05 '17 at 17:11
  • I'm thinking another if statement, using i > 1 and [i-2] nested within the first if? – Saint Razz Sep 05 '17 at 17:15
  • 1
    I appreciate the help again. The second if statement worked. A quibble with Codefights is that you can't see any solutions until you get through the problem. I suppose there is a logic in that, but sometimes I wonder if getting stuck isn't less productive than viewing a good answer and digesting it before moving on. On the other hand, sometimes the practice you get at research can be as valuable as the practice coding. Thanks, all! o7 – Saint Razz Sep 05 '17 at 17:25