1

I have the following class:

public class MyClass
{
   public MyArrObj[] arrOfObj; //Array of objects
}

When I try to access it using:

MyClass myClass = new MyClass;
myClass.arrOfObj = GetData();

it gives me a null exception. If it wasn't an array, I could have new'd it, but i am not sure how to handle this.

John Saunders
  • 157,405
  • 24
  • 229
  • 388
Dave S
  • 89
  • 1
  • 6

4 Answers4

2

When creating an array of a reference type (MyArrObj in your case), it is required that you allocate memory for each array element (using new operator) in addition to allocating memory for the array itself.

Array of Reference types

For example, if you create an MyArrObj array,

MyArrObj[] objectArray = new MyArrObj[3];

Then the array elements objectArray[0] to objectArray[2] would still be null. A separate initialization is required.

objectArray[0] = new MyArrObj();

Only when the above step is done can you access the members of the array element.

objectArray[0].SomeMethod();
SomePropertyType readProperty = objectArray[0].SomeProperty;

If you skip the array element initialization then trying to access a member of the array element would give a System.NullReferenceException

objectArray[0].SomeMethod(); // throws NullReferenceException

because objectArray[0]; is null.

You can check this using

if(objectArray[0] == null)
{
    Console.WriteLine("objectArray[0] is null");
}

Array of Value types

If you know the array before hand, then you can use the initialization pointed out by @clonked.
For value types like built in types(like int, float, structs, etc. you need not initialize each array element.
For example, if you create an int array,

int[] intArray = new int[3];

Then the array elements intArray[0] to intArray[2] have the memoery allocated and values can be assigned to them.

intArray [0] = 1;

Check your GetData()

So your GetData() method should include code like this,

private MyArrObj[] GetData()
{
    int numberOfObjects = GetNumberOfObjects(); // Get the number of objects at runtime

    MyArrObj[] objectArray = new MyArrObj[numberOfObjects];

    for (int i = 0; i < numberOfObjects; i++)
    {
        objectArray[i] = new MyArrObj();
    }

    return objectArray;
}



More information

Devendra D. Chavan
  • 8,269
  • 4
  • 28
  • 33
0

The way your are using your GetData() method, it has to return an array.

Here's a couple of ways to create an array on the fly (you'll still need to populate them, though):

int[] x = new int[5] ; // create a 5-element  array of integers
int[] y = (int[]) Array.CreateInstance(typeof(int),5) ; // create a 5 element array of integers

Cheers.

Nicholas Carey
  • 60,260
  • 12
  • 84
  • 126
0

You need to do something like this:

class Program
    {
        static void Main(string[] args)
        {
            MyClass myClass = new MyClass();
            myClass.arrOfObj = GetData();
            foreach (var item in myClass.arrOfObj)
            {
                Console.WriteLine(item.ToString());
            }

        Console.ReadLine();

    }

    private static int[] GetData()
    {
        return new int[]{1,2,3,4,5};
    }
}

public class MyClass
{
    public int[] arrOfObj; //Array of objects
}

the GetData method is the important part. it has to initialize a new array and return it.

Notter
  • 572
  • 4
  • 15
0

If the signature of your GetData() method looks like public MyArrObj[] GetData() then your code should work correctly. I tested your code with 2 versions of the 'GetData' method:

public MyArrObj[] GetData()
{
    return new MyArrObj[3] { new MyArrObj(), new MyArrObj(), new MyArrObj() };
}

public MyArrObj[] GetData2()
{
    return new MyArrObj[0];
}

Neither of these methods threw a null reference exception. I would suspect there is something else inside of your GetData() method that is throwing the null reference, rather than the operation of assigning the results of that method to the arrOfObj field.

Nathan Anderson
  • 6,522
  • 23
  • 29