java.lang.ArrayIndexOutOfBoundsException error when multiply matrices
Hello everyone. So the problem that I have is when I try to do this part of my code : cMatrix m2 = new cMatrix(10,20); m2.AssignRandom(); cMatrix m3 = new cMatrix(20,10); m3.AssignRandom(); cMatrix m4 = new cMatrix(); m4 = m2.multiplyMatrices(m3); m4.printMatrixWithPrime(); I get the error on the title. Letme show you my "multiplyMatrices" method. public cMatrix multiplyMatrices(cMatrix Multiplicand){ long sum = 0; for (int i = 0; i < Multiplicand.row; i++){ for (int j = 0; j < Multiplicand.col; j++){ for (int k = 0; k < col; k++){ sum = sum + elements[i][k] + elements[k][j]; } Multiplicand.elements[i][j] = sum; sum = 0; } } return null; } For your information : public cMatrix(){ row = 10; col = 10; elements = new long [row][col]; } public cMatrix(int row, int col){ this.row = row; this.col = col; elements = new long [row][col]; } those are the constructors. Every other methods are working without errors but the multiplaction gives me the eror on the title and compiler says that : sum = sum + elements[i][k] + elements[k][j]; m4 = m2.multiplyMatrices(m3); this two has got errors. Can someone help ?