-2

I want to create a multidimentional array with Fibonacci numbers. I need to create rows with odd numbers. After compiling I receive NullPointerException. Could You help me find solution for this issue?

public class Scratchpad01 
{
public static int fib(int n)
    {           
    if(n <= 1) 
        {
            return n;
        }
        else
        {
            return fib(n-1) + fib(n-2);
        }
    }

public static void main (String args[]) 
    {
    int[][] tab = new int[11][];
    int count;

    for(int i=1; i<11; i++)
       {
        if(fib(i) % 2 == 0) continue;
        else tab[i] = new int[fib(i)];          
        }

    for(int i=1; i<11; i++)
       {
        count = tab[i].length;
        for(int j=0; j<tab[i].length; j++){             
            tab[i][j] = count--;
       }
    }

    for(int i=1; i<11; i++)
       {
        for(int j=0; j<tab[i].length; j++)
            {
            System.out.print(tab[i][j] + " ");
           }
        System.out.println("");
        }
}
}

After deleting this lineif(fib(i) % 2 == 0) continue; I receive:

1 1 2 1 3 2 1 5 4 3 2 1 8 7 6 5 4 3 2 1 13 12 11 10 9 8 7 6 5 4 3 2 1 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

but I need to have:

1 1 3 2 1 5 4 3 2 1 13 12 11 10 9 8 7 6 5 4 3 2 1 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 ...

Matt
  • 1
  • 3

2 Answers2

2

Here in your first for loop if fib(i) % 2 is equal to zero then you are not initializing that index of element of array

And in next loop you are accessing n array which is even not initialized.

Ascalonian
  • 14,409
  • 18
  • 71
  • 103
Dipen Adroja
  • 415
  • 5
  • 15
0

I've found solution for this issue. Here is complete code:

public class Scratch01 {
public static int fib(int n){           
    if(n <= 1){
            return 1;
        }
        else{
            return fib(n-1) + fib(n-2);
        }
    }

public static void main (String args[]) {
    int[][] tab = new int[10][];
    int count;

    for(int i=0; i<tab.length; i++){
        if(fib(i) % 2 == 0) continue;
        tab[i] = new int[fib(i)];           
    }

    for(int i=0; i<tab.length; i++){
        if(fib(i) % 2 == 0) continue;
        count = tab[i].length;
        for(int j=0; j<tab[i].length; j++){             
            tab[i][j] = count--;
        }
    }

    for(int i=0; i<tab.length; i++){
        if(fib(i) % 2 == 0) continue;
        for(int j=0; j<tab[i].length; j++){
            System.out.print(tab[i][j] + " ");
        }
        System.out.println("");
    }
}

}

And the result:

1 1 3 2 1 5 4 3 2 1 13 12 11 10 9 8 7 6 5 4 3 2 1 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

Thank you for help. Best regards Matt

Matt
  • 1
  • 3