-1
    public static double[][] ConvertToDouble(int[,] arr , int r , int c)
    {
        double[][] matrix = new double[r][c];
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
                matrix[i][j] = Convert.ToDouble(arr[i,j]);  //the error comes in this line
            }
        }

        return matrix;
    }

iam here trying to create a function that converts int[,] to double[][] and i got a System.NullReferenceException

  • 2
    You are passing a null array of int into your method. – Zohar Peled Mar 14 '20 at 12:06
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Pavel Anikhouski Mar 14 '20 at 12:08
  • no iam sure the array isn't null – Marox587 Mar 14 '20 at 12:09
  • 2
    This code cannot compile, the matrix[][] initialization is not correct. Also a likely cause for the NRE. Google "c# initialize jagged array" to get ahead. – Hans Passant Mar 14 '20 at 12:10
  • how to initialize it correctly ? – Marox587 Mar 14 '20 at 12:16
  • 1
    You should **1.** initialize matrix using next code: `double[][] matrix = new double[r][];`; **2**. and then inside the first for loop (before the second loop) initialize each line of the matrix: `matrix[i] = new double[c];`. See [jagged arrays](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/jagged-arrays). – Iliar Turdushev Mar 14 '20 at 12:18
  • it wokrs thank you – Marox587 Mar 14 '20 at 12:34

2 Answers2

0

Before you can use matrix, its elements must be initialized. You can initialize the elements like this:

double[][] matrix = new double[r][];
for (int i = 0; i < r; i++) {
    matrix[i] = new double[c];
}

Complete Code:

public static double[][] ConvertToDouble (int[, ] arr, int r, int c) {
            double[][] matrix = new double[r][];
            for (int i = 0; i < r; i++) {
                matrix[i] = new double[c];
            }

            for (int i = 0; i < r; i++) {
                for (int j = 0; j < c; j++) {
                    matrix[i][j] = Convert.ToDouble (arr[i, j]);
                }
            }

            return matrix;
        }

Please read this Microsoft's official documentation.

0

You have to initialize the double[][] in stages. We can do this by adding only one line of code and changing one other:

public static double[][] ConvertToDouble(int[,] arr , int r , int c)
{
    double[][] matrix = new double[r][];
    for(int i=0;i<r;i++)
    {
        matrix[i] = new double[c];
        for(int j=0;j<c;j++)
        {
            matrix[i][j] = Convert.ToDouble(arr[i,j]);  //the error comes in this line
        }
    }

    return matrix;
}

We can also make the method easier to call by inferring r and c:

public static double[][] ConvertToDouble(int[,] arr)
{
    //GetUpperBound() returns the index of the last item, rather than the number of items
    var r = arr.GetUpperBound(0) + 1;
    var c = arr.GetUpperBound(1) + 1;

    double[][] matrix = new double[r][];
    for(int i=0;i<r;i++)
    {
        matrix[i] = new double[c];
        for(int j=0;j<c;j++)
        {
            matrix[i][j] = Convert.ToDouble(arr[i,j]);  //the error comes in this line
        }
    }

    return matrix;
}
Joel Coehoorn
  • 362,140
  • 107
  • 528
  • 764