dragonmnl
dragonmnl

Reputation: 15538

2-dimensional array assignment: "reference to an object was not set for an instance of the object"

I'm getting "reference to an object was not set for an instance of the object" run-time error with the suggestion: "Use the new keyword to create an object instance. But I don't understand what I'm doing wrong.

There is my code:

int numListas = 10;    
x = 15;
int[][] listas;
listas = new int[numListas][];
Random random = new Random(); // generate random number
for (idx = 0; idx < numListas; idx++)
{
 int idx_el;
 for (idx_el = 0; idx_el < x ; idx_el++)
 {
  listas[idx][idx_el] = random.Next(0,100);
 }
}

Upvotes: 1

Views: 1020

Answers (4)

Jon Skeet
Jon Skeet

Reputation: 1500145

This line:

int[][] listas;

declares a variable which is a reference to an array of arrays.

This line:

listas = new int[numListas][];

initializes the variable to refer to an array of numListas array references... but each of those "subarray" references is null to start with. You need to initialize each "subarray", probably in your outer loop:

for (idx = 0; idx < numListas; idx++)
{
    listas[idx] = new int[x];
    ...
}

Note that an alternative is to use a rectangular array instead of an array of arrays:

int[,] listas = new int[numListas, x];
...

// In your loop
listas[idx, idx_el] = random.Next(0, 100);

As an aside, code generally reads more clearly when you declare and initialize variables together, rather than declaring them earlier and then initializing them later. Also, underscores in C# variable names aren't terribly idiomatic, and the overall variable names aren't terribly clear. I'd write your code as:

// Consider declaring these as constants outside the method
int rowCount = 10;
int columnCount = 15;

int[][] values = new int[rowCount];
Random random = new Random();
for (int row = 0; row < rowCount; row++)
{
    values[row] = new int[columnCount];
    for (int column = 0; column < columnCount; column++)
    {
        values[row][column] = random.Next(100);
    }
}

You should also consider initializing a single Random instance outside the method and passing it in - see my article on Random for more information.

Upvotes: 1

Femaref
Femaref

Reputation: 61437

You don't set a dimension for the second dimension listas = new int[numListas][];.

As you are using x as the limit of your loop, use that as dimension:

for (idx = 0; idx < numListas; idx++)
{
 int idx_el;
 listas[idx] = new int[x]; // initialize the second dimension
 for (idx_el = 0; idx_el < x ; idx_el++)
 {
  listas[idx][idx_el] = random.Next(0,100);
 }
}

Alternatively, you can use a rectangular array at the beginning:

int[,] listas = new int[numListas,x];

Upvotes: 1

Douglas
Douglas

Reputation: 54877

You need to initialize each inner array within your jagged array:

int numListas = 10;    
x = 15;
int[][] listas;
listas = new int[numListas][];
Random random = new Random(); // generate random number
for (idx = 0; idx < numListas; idx++)
{
 listas[idx] = new int[x];   // initialize inner array
 int idx_el;
 for (idx_el = 0; idx_el < x ; idx_el++)
 {
  listas[idx][idx_el] = random.Next(0,100);
 }
}

Alternatively, you may use a two-dimensional array:

int numListas = 10;    
x = 15;
int[,] listas;
listas = new int[numListas,x];   // 2D array
Random random = new Random(); // generate random number
for (idx = 0; idx < numListas; idx++)
{
 int idx_el;
 for (idx_el = 0; idx_el < x ; idx_el++)
 {
  listas[idx,idx_el] = random.Next(0,100);
 }
}

Upvotes: 1

Avner Shahar-Kashtan
Avner Shahar-Kashtan

Reputation: 14700

Did you forget to initialize the second dimension of the array?

listas = new int[numListas][x];
            initialize this ^   

Upvotes: 0

Related Questions