List as a field of an object. [Answered]
What am I doing wrong? I'm trying to have a Character object with ever increasing amount of Names.
9 Replies
I'm assuming this throws a null reference exception? you aren't initializing Name to anything
System.NullReferenceException: 'Object reference not set to an instance of an object.'
But does it make sense what I'm trying to do? How would you do it?
that happens because
List<string> Name
starts as null
unless you add = new List<string>();
(also, you probably want Name.Add("test")
instead of Name[0] = "test"
)
I added it in the constructor.
that works, although you can just do
Ah, I understand.
Thanks @Windows10CE.
Ask the thread owner or member with permission to close this!
damn
✅ This post has been marked as answered!