I have a NullReferenceException which i do not understand maybe it’s easy to explain.
The error is :
NullReferenceException: Object reference not set to an instance of an object
JsonStam.Start () (at Assets/JsonStam.cs:49)
I am assigning :
stam4.list[0].path = " blat";
I even made the constructor allocates the memory explicitly
[Serializable]
public class StamClass {
public Picture[ ] list;
public StamClass() {
list = new Picture[2];
}
}
[Serializable]
public class Picture {
public string path;
public string title;
}
What is the problem?
File Attached.
3126093–236794–JsonStam.cs (748 Bytes)
larku
June 29, 2017, 9:47am
2
Your array has been allocated (list = new Picture[2]
) but the elements in it have not. Since a class
is a reference type you need to allocate each of them individually.
Or change Picture to be a struct
instead of a class
and it should work since structs are value types and don’t need to be allocated with new
.
1 Like
I would put a constructor inside picture and put in the stamclass some values like
Picture b = new Picture(" text ", " text ");
or something like that
… so you would create an array of pictures and say
public Picture[ ] list;
…
list = new Picture[2];
and than for each picture in the aray you create a new object like the first line above…
I hope I don´t sound too complicated
so here you go:
[Serializable]
public class StamClass {
public Picture[ ] list;
public StamClass() {
list = new Picture[2];
list [0] = new Picture (“”,“”);
}
public int Length() {
return(list.Length);
}
}
[Serializable]
public class Picture {
public string path;
public string title;
public Picture(string path2,string title2)
{
path = path2;
title = title2;
}
}
just change those two classes with my version above
My Problem was as Larku mentioned allocating the pointer doesnt allocate the class instance.
Thanks