0

I have a Serializable Class, I want to create its array in other script

[Serializable]
public class Question
{
    public string fact;    
}

Now here I have another class that creates and initializes array of Question.

public class Initialize : MonoBehaviour
{
    Question[] questions;
    void Start()
    {
        questions = new Question[1];
        questions[0].fact = "First Question"; // Here I am getting NullReferenceException
    }
}

Another interesting thing to mention is when I use IEnumerator to initialize same array and yield return null at beginning the code works as expected, Here is the code

 public class Initialize : MonoBehaviour
 {
     Question[] questions;
     void Start()
     {
         questions = new Question[1];
         StartCoroutine(InitializeArray());
     }

    IEnumerator InitializeArray()
    {
        yield return null;
        questions[0].fact = "First Question";
    }
}

When I use yield statement before initializing it works fine, when I comment out yield statement again NullReferenceException strikes, How this is even possible?

Juned Khan Momin
  • 2,028
  • 10
  • 30
  • 1
    Possible duplicate of [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) – Dour High Arch Jan 06 '19 at 21:00
  • Possible duplicate of [Null Reference Exception when calling an Object Array](https://stackoverflow.com/questions/15102929/null-reference-exception-when-calling-an-object-array) – mjwills Jan 06 '19 at 21:37

3 Answers3

2

When you declare a reference type array, each element is initialized as null. This is why you are getting a NullReferenceException when you try to access the property of one of the array elements.

To avoid this, you can initialize the array with the initialized reference type(S):

Question[] questions;
questions = new Question[] 
{
    new Question
    {
        fact = "First Question"
    },
    new Question
    {
        fact = "Second Question"
    }
};
devNull
  • 2,753
  • 1
  • 10
  • 10
  • can you specify how I would initialize an array of 2 elements, your code isn't working. Can you please give me code to initialize array of two elements? – Juned Khan Momin Jan 06 '19 at 20:57
  • 1
    @JunedKhanMomin I updated the answer to include an example of initializing the array with multiple elements – devNull Jan 06 '19 at 21:05
  • this works, thanks, isn't there some simple method to do it, as for big arrays this will be very tedious way to initialize array. – Juned Khan Momin Jan 06 '19 at 21:30
  • `questions[0]=new Question();` if you had a larger array you could initialize it all using a loop: `for(int i=0; i – pinkfloydx33 Jan 06 '19 at 23:52
  • @pinkfloydx33, There was no need for your comment. Already accepted answer does that See Here: https://stackoverflow.com/a/54072338/8304176 – Juned Khan Momin Jan 09 '19 at 12:48
  • At the time of writing my comment, no answer initialized via a loop and it was in direct response to your previous comment "isn't there some simple method..." – pinkfloydx33 Jan 09 '19 at 12:54
  • Also, nothing in the original question specified that the array needed any sort of loop initialization – devNull Jan 09 '19 at 15:58
1

When you call

questions = new Question[1];

you are instantiating an empty Question array with a size of 1, thus the first element will contain null. To make your code work you need to populate it with a Question instance, like so:

questions = new[] { new Question() };

Your second code snippet will fail for the same reason; you're creating an array with no Question objects in it. The yield return null is exiting the InitializeArray method early, which stops the failing code from being run.

Castrohenge
  • 7,390
  • 5
  • 33
  • 56
0

Best way to initialize Array of your own custom class:

 public class Initialize : MonoBehaviour
 {
    Question[] questions;

    void Start()
    {
       questions = new Question[2];

       questions[0] = new Question();
       questions[0].fact = "First Question";

       questions[1] = new Question();
       questions[1].fact = "Second Question";
    }
}

If there are multiple elements to Initialize, we can use loop just like:

questions = new Question[100];
for (int i = 0; i < questions.Length; i++)
{
    questions[i] = new Question();
    questions[i].fact = "Question Number " + i;
}
Juned Khan Momin
  • 2,028
  • 10
  • 30