Good day. I am writing a code that considers the remainder of dividing the Fibonacci number through matrices. On small n with long'es everything worked, when I switched to BigInteger, everything crashes.

Issues:

Exception in thread "main" java.lang.NullPointerException 

It seems everything is initialized, I do not understand what the problem is. Tell me, please, what is the problem. Here is the code: http://pastebin.com/Q63qdVRW

  • Attach error and code to question, please - Senior Pomidor

1 answer 1

problem in the next code section

 BigInteger[][] mResult = new BigInteger[mRRowLength][mRColLength]; for(int i = 0; i < mRRowLength; i++) { // rows from m1 for(int j = 0; j < mRColLength; j++) { // columns from m2 for(int k = 0; k < m1ColLength; k++) { // columns from m1 mResult[i][j] = mResult[i][j].add(m1[i][k].multiply(m2[k][j])); } } } 

initialized ONLY the mResult array, but the elements did not. go through the cycle and refer to the elements of mResult.

mResult[i][j].add(m1[i][k].multiply(m2[k] - here we want to assign a certain value to the elements of the array, only here mResult[i][j] == null, therefore get NullPointerException

  • thank you I have a feeling that I am completely stupid. Did not quite understand, but in this case how to initialize the elements? - TheDoctor
  • @TheDocto no, 0 is impossible, since the multiplication operation. by meaning it turns out that there should be mResult[i][j] = m1[i][k].multiply(m2[k][j])); - Senior Pomidor
  • @Senior Pomidor, thank you very much for the clarification, that's all, I get it! - TheDoctor