【问题标题】:nullpointer exception in 2D randomly generated array (gaussian elimination) [duplicate]二维随机生成数组中的空指针异常(高斯消除)[重复]
【发布时间】:2018-05-05 05:19:38
【问题描述】:

我在随机生成二维数组时发现了一个空指针异常。我以前用过同样的方法,它从来没有给我一个空指针异常。

这里是主要方法。

    public static void main(String[] args) 
    {
    int row = 5;
    int column = 6;

    double[][] matrix = new double[row][column];

    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < column; j++)
            matrix[i][j] = Math.random()*20;
    }
    b = getB(matrix, row, column);
    printArray(matrix);
    System.out.println("");
    System.out.println("");


    elimination(row, column);
    backSubstitution(row,column);
    printSolution(column);

}

调试器告诉我,导致异常的那一行在排除方法里面的findMaxPivotRow方法。

public static void elimination(int rows, int cols)
{
    int pivotRow;
    double multiplier;

    for (int r = 0; r< rows; r++)
    {
        pivotRow = findMaxPivotRow(r, rows);
        swapRow(r, pivotRow);

        for (int i = r+1; i < rows; i++)
        {
            multiplier = matrix[i][r] / matrix[r][r];
            b[i] -= (multiplier * b[r]);

            for (int j = r; j < rows; j++)
            {
                matrix[i][j] -= (multiplier * matrix[r][j]);
            }
        }
    }
}

public static int findMaxPivotRow(int k, int rows)
{
    double max_value = matrix[k][k];
    int max_row = k;

    for (int r = k; r < rows; r++)
    {
        if (Math.abs(matrix[r][k]) > max_value)
        {
            max_value = matrix[r][k];
            max_row = r;
        }
    }
    return max_row;
}

public static void swapRow(int k, int pivotRow)
{
    double[] swap;
    double multiplier;
    double swap_b;

    swap = matrix[k];
    matrix[k] = matrix[pivotRow];
    matrix[pivotRow] = swap;

    swap_b = b[k];
    b[k] = b[pivotRow];
    b[pivotRow] = swap_b;
}

导致异常的行就是这一行

double max_value =  matrix[k][k];

我在线搜索了二维数组中的空指针异常,它告诉我错误是数组未初始化或代码超出范围。我非常彻底地使用了调试器,它向我表明索引位于正确的位置。

我知道我向你扔了很多代码,但你可以阅读 main 方法、前几行排除法和 findMaxPivotRow 方法。

如果你能找到为什么该行导致空指针异常,那将非常有帮助。谢谢。

【问题讨论】:

  • NullPointerException 不能是由 Java 中的代码超出数组边界引起的。您找到的信息不正确。

标签: java multidimensional-array nullpointerexception


【解决方案1】:

您必须将main() 中声明的double[][] 'matrix' 变量传递给其他方法,以便他们可以使用它或将该变量保留为静态成员,以便所有静态方法都可以访问。

【讨论】:

  • 感谢您提供的信息,原来我创建了两个不同的数组,称为矩阵。一个作为全局方法,一个在 main 方法中。
【解决方案2】:

二维数组matrix 未在您的方法findMaxPivotRow 中定义。 您需要传递在 main 方法中声明的矩阵,或者需要将其声明为全局变量。

您可以在我声明为全局变量的地方看到这段代码。

public class myClass{
    static double matrix[][];
    public static void main(String[] args) 
    {
        int row = 5;
    int column = 6;
        matrix = new double[row][column];

    for(int i = 0; i < row; i++)
        {
           for(int j = 0; j < column; j++)
               matrix[i][j] = Math.random()*20;
        }
        b = getB(matrix, row, column);
        printArray(matrix);
        System.out.println("");
        System.out.println("");


        elimination(row, column);
        backSubstitution(row,column);
        printSolution(column);
    }   
    public static void elimination(int rows, int cols)
    {
        int pivotRow;
        double multiplier;

        for (int r = 0; r< rows; r++)
        {
            pivotRow = findMaxPivotRow(r, rows);
            swapRow(r, pivotRow);

            for (int i = r+1; i < rows; i++)
            {
                multiplier = matrix[i][r] / matrix[r][r];
                b[i] -= (multiplier * b[r]);

                for (int j = r; j < rows; j++)
                {
                    matrix[i][j] -= (multiplier * matrix[r][j]);
                }
            }
            }   
   }    

   public static int findMaxPivotRow(int k, int rows)
   {
     double max_value = matrix[k][k];
     int max_row = k;

    for (int r = k; r < rows; r++)
        {
            if (Math.abs(matrix[r][k]) > max_value)
            {
                max_value = matrix[r][k];
                max_row = r;
             }
        }
        return max_row;
   }
}

public static void swapRow(int k, int pivotRow)
{
    double[] swap;
    double multiplier;
    double swap_b;

    swap = matrix[k];
    matrix[k] = matrix[pivotRow];
    matrix[pivotRow] = swap;

    swap_b = b[k];
    b[k] = b[pivotRow];
    b[pivotRow] = swap_b;
}

【讨论】:

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