0

So, I try to automate this script for Euler project 15. I don't understand why in this version the automation won't work, in the last piece of code.

    BigInteger b = new BigInteger("1");
    BigInteger Arr [][] = new BigInteger [20][20];
    //add 1 to 20.
    for(int i=1; i<=20;i++) {
        String I = Integer.toString(i);
        b = new BigInteger(I);
        Arr[1][i-1] = b;
    }

The following piece of code works. So i try automated it, which does not work. Help me find out why.

    Arr[2][1] = Arr[1][2];
    for(int i = 2; i<20; i++) {
        Arr[2][i] = Arr[2][i-1].add(Arr[1][i]);
        System.out.println(Arr[2][i]);
    }
    Arr[3][2] = Arr[2][3];
    for(int i = 3; i<20; i++) {
        Arr[3][i] = Arr[3][i-1].add(Arr[2][i]);
        System.out.println(Arr[3][i]);
    }
    Arr[4][3] = Arr[3][4];
    for(int i = 4; i<20; i++) {
        Arr[4][i] = Arr[4][i-1].add(Arr[3][i]);
        System.out.println(Arr[4][i]);

    }

Here I tried to automate the code. but it gives me an:

Exception in thread "main" java.lang.NullPointerException at Problem15.main(Problem15.java:45)

for(int c=2; c<20; c++) {
    Arr[c][c-1] = Arr[c-1][c];

    for(int i = c; i<20; c++) {
        System.out.println(Arr[c][i-1]);
        Arr[c][i] = Arr[c][i-1].add(Arr[c-1][i]);
        System.out.println(Arr[c][i]);
    }
    System.out.println("hi");
}
izhang05
  • 744
  • 2
  • 11
  • 26
  • Could you precise to us which line is faulty ? Would be quite usefull seeing how many index you have there. – Lutzi Jan 22 '20 at 19:51
  • 1
    What do you mean by "automated it"? – mypetlion Jan 22 '20 at 19:52
  • Have you tried debugging it to see the offending values of the array? – Compass Jan 22 '20 at 19:53
  • you have a null pointer exception at line 45, without line numbers we can only guess where (or understand the code for you). My guess would be this line: > Arr[c][i] = Arr[c][i-1].add(Arr[c-1][i]); Identify the correct line, put a debugger break point there and debug your program. You will find that some variable is null, but you try to call an operation on it. That's your problem. And likely some object in your array isn't initialized like it should. – Frank Hopkins Jan 22 '20 at 19:53
  • 2
    To follow up on @FrankHopkins' comment, if your first snippet of code is how you're initializing the values in `Arr`, then you're only initializing 20 of the elements, but there are 400 elements in a 20x20 array, which means plenty of nulls. – Jordan Jan 22 '20 at 19:55
  • What you need to do is generously sprinkle print statements through out your program and trace the values and the indices. And it's not the `for()` statement that is the problem. It is working just fine. – WJS Jan 22 '20 at 19:59
  • @Lutzi The last bit of code works for one iteration of for(int i =c....) but after the first iteration, it does not work anymore and gives me the error. – casper belier Jan 22 '20 at 20:02
  • 1
    I assume this is a typo? `(int i = c; i<20; c++)`, should be `i++`? – Nexevis Jan 22 '20 at 20:03
  • @casperbelier What I meant was : you have a problem at the 45th line of your file. Which line is that ? – Lutzi Jan 22 '20 at 20:07
  • @All I have found the problem, sorry for bothering you. the problem was i had was that the last for() statement had c++ at the end which should have been i++. thank you for the support. – casper belier Jan 22 '20 at 20:20

0 Answers0