Creating instance of class that does not extend MonoBehaviour

I tried to make a simple class to store data during runtime. I did not want to attach it to a GameObject because it will be deleted almost right after it was made. I made it to convert into a JSON string and get sent to client-side javascript.

Data Class:

public class ExampleData {
            public string[] target;
            public string sender;
            public string type;
}

The problem - I was trying to test out the JSON string conversion but the class is not registering. Because it does not extend Monobehaviour, I tried to treat it like a class in regular programming by using the keyword β€œnew”. This is where the error is:

void TestData() {
        ExampleData exData = new ExampleData(); 
        exData.target[0] = "all"; //exData is not set to an instance?? Should be
        exData.sender = "a_name";
        exData.type = "test";
        string updateData = JsonUtility.ToJson(exData);
        Debug.Log(updateData);
}

Everything is picked up by Visual Studio fine. With no errors, but Unity says this message:

NullReferenceException: Object reference not set to an instance of an object
(wrapper stelemref) object:stelemref (object,intptr,object)

Any advice would be greatly appreciated. Thank you.

Just a basic array problem. ExampleData exData = new ExampleData(); creates an instance of the class with an empty array. Then exData.target[0] = "all"; is trying to insert a value at index [0] which does not exist.

Try this –

 ExampleData exData = new ExampleData()
        {
            target = new string[1]
        };
        exData.target[0] = "all"; 
        exData.sender = "a_name";
        exData.type = "test";
  string updateData = JsonUtility.ToJson(exData);
         Debug.Log(updateData);