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
...