About 2DList C#

This my Code:

[System.Serializable]
    public class IntList
    {
        public List<int> Intlist = new List<int>();
    }
    public IntList[] TypeSomething= newIntList[1] ;

Then i take this

TypeSomething[0].Intlist.Add(0);

why can’t add it ?

it Error:

NullReferenceException: Object reference not set to an instance of an object

Sorry for my English And thank you

Firstly, please use code tags: http://forum.unity3d.com/threads/143875-Using-code-tags-properly
Next, you really shouldn’t name your variable Type. (I need someone advanced to explain it haha. I suck at explaining this)
Lastly, when you created your array, the object inside is null (At least I remember it being null). So you are accessing a null value in the array, hence, null exception error.

Thank you for aswer

Why I take this :

public List<int> Intlist = new List<int>();

Then i

intlist.Add(0);

it no Error Spite it’s null

Because you’re creating an empty list. So when you call List.Add, it’ll just add into the empty list, not replacing a value. But in your first example, you are calling a null value from an array.

TypeSomething[0]

is null because there’s nothing in it. So when you do

TypeSomething[0].Intlist.Add(0);

You are trying to reference a null value as TypeSomething[0] is null.

Thank you

But i i take :

public IntList[] TypeSomething = newIntList[1] ;

Before i

TypeSomething[0].Add(0);

I think “element 0” in TypeSomething[0] it isn’t null

Can You Help Me For Fixed Error it?

Thank you

You missed a space after new btw.

public IntList[] TypeSomething = new IntList[1] ;
TypeSomething[0] = new IntList();
TypeSomething[0].IntList.Add(0);

Something like that I think.

Another way you could go about it is to not create the list using the constructor in the class.

[System.Serializable]
public class IntList
{
	public List<int> IntList;
}
public IntList[] TypeSomething;

void Start()
{
	TypeSomething = new IntList[1];
	TypeSomething[0] = new IntList();
	TypeSomething[0].IntList = new List<int>();
	TypeSomething[0].IntList.Add(0);
}

Or maybe I just can’t explain stuff =(

Ok it work thank you very much