Error happened:
NullReferenceException: Object reference not set to an instance of an object
JSON :
{
"Fleets": [
[
{ "id": 1 },
{ "id": 2 }
],
[
{ "id": 1 },
{ "id": 2 }
]
]
}
Class :
[Serializable]
public class TestShip
{
public int id;
}
[Serializable]
public class TestClass
{
public List<List<TestShip>> Fleets;
}
Test code:
TestClass b= LoadJson.LoadJsonFromFile<TestClass>(LoadJson.defaultPath);
Debug.Log(b.Fleets[0][0].id);
the method is correct, dont worry about it. The code is here:
public static T LoadJsonFromFile<T>(string path)
{
if (!File.Exists(path))
{
Debug.Log("nothing");
return default(T);
}
StreamReader sr = new StreamReader(path);
if (sr == null)
{
return default(T);
}
string json = sr.ReadToEnd();
if (json.Length > 0)
{
Debug.Log(json.Length);
Debug.Log(json);
return JsonUtility.FromJson<T>(json);
}
return default(T);
}
Who can solve this problem, please!!!