【问题标题】:How to resolve NullPointerException in multidimentional array?如何解决多维数组中的空指针异常?
【发布时间】:2015-04-19 18:24:04
【问题描述】:

我想用斐波那契数创建一个多维数组。我需要创建奇数行。编译后我收到 NullPointerException。你能帮我找到这个问题的解决方案吗?

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("");
        }
}
}

删除此行后if(fib(i) % 2 == 0) continue;收到:

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

但我需要:

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

【问题讨论】:

  • 从哪里获得 NPE?
  • 我在第 23 行得到 NPE,即“count = tab[i].length;”

标签: java arrays multidimensional-array nullpointerexception null-pointer


【解决方案1】:

我已经找到了解决这个问题的方法。这是完整的代码:

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("");
    }
}

}

结果:

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

感谢您的帮助。 最好的祝福 马特

【讨论】:

    【解决方案2】:

    在你的第一个 for 循环中,if fib(i) % 2 等于零,那么你没有初始化数组元素的索引

    在下一个循环中,您正在访问甚至未初始化的 n 数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      • 1970-01-01
      • 2014-12-02
      • 2012-02-05
      相关资源
      最近更新 更多