尝试将值赋给二维数组时出现Null Reference Exception

时间:2013-05-26 12:20:16

标签: c# multidimensional-array variable-assignment

嗨我有一项任务,我需要使用Gausian消除方法解决线性方程组。

到目前为止,这是我的代码:

 private static double[][] equation = new double[][]
 {
  new double[] {0.0, 8.0, 2.0, -7.0},   
  new double[] {3.0, 5.0, 2.0, 8.0},
  new double[] {6.0, 2.0, 8.0, 26.0}    
 };

 private const int rowSize = 3;
 private const int columnSize = 4;
 private static int pivotRow;
 private static int curentRowIndex;

 public static void GetTriangularMatrix()
 public static void GetTriangularMatrix()
    {
        PrintMatrix();

        for (int k = 0; k <= columnSize - 2; k++)
        {
            curentRowIndex = k;
            double pivot = GetPivot(k);
            SwapCurentRowWithPivotRow(pivot);
            FindTriangularMatrix();
        }
    }

    public static double GetPivot(int curentPivot)
    {
        double pivot = equation[curentPivot][curentPivot];
        pivotRow = curentPivot;

        for (int i = curentPivot; i <= rowSize - 1; i++)
        {
            if (pivot < equation[i][curentPivot])
            {
                pivot = equation[i][curentPivot];
                pivotRow = i;
            }
        }

        Console.WriteLine("Pivot Row is " + (pivotRow + 1));
        Console.WriteLine("Pivot is " + pivot);

        return pivot;
    }

    private static void SwapCurentRowWithPivotRow(double pivot)
    { 
        if(pivot != equation[curentRowIndex][curentRowIndex])
        {
            double temp;
            for (int i = 0; i <= columnSize - 1; i++)
            {
                temp = equation[curentRowIndex][i];
                equation[curentRowIndex][i] = equation[pivotRow][i];
                equation[pivotRow][i] = temp;
            }
        }

        PrintMatrix();
    }

    public static void FindTriangularMatrix()
    {
        double[][] trangularMatrix = new double[rowSize][];

        for (int j = curentRowIndex + 1; j <= rowSize - 1; j++)
        {
            trangularMatrix[j][curentRowIndex] = equation[j][curentRowIndex] / equation[curentRowIndex][curentRowIndex];
        }
    }

当我尝试为FindTriangularMatrix()分配一些值时{@ 1}}方法,我得到一个空引用异常。

我认为这更像是一个语法问题,它可能是我遗漏的东西,因为我之前从未使用过二维数组。

任何人都可以告诉我我做错了什么以及如何纠正?

0 个答案:

没有答案