2
我有序列化問題。我希望保存並加載一塊簡單的3D數據。我有一個包含尺寸(寬度,高度和長度)以及3D整數數組的類。 JSON很高興地將這個類轉換爲一個字符串,以供我保存,但是當它再次轉換時它不會很好地播放。使用JSON時類中的數組出現問題
的數據類:
Public class cClusterData {
public int mWidth;
public int mHeight;
public int mLength;
public int[,,] mCellType;
public cClusterData()
{
mCellType = new int[32,32,32];
}
}
的例行程序,將其保存:
public void SaveCluster()
{
cClusterData lData = new cClusterData();
lData.mWidth = mWidth;
lData.mHeight = mHeight;
lData.mLength = mLength;
for (int lX = 0; lX < mWidth; lX++)
{
for (int lY = 0; lY < mHeight; lY++)
{
for (int lZ = 0; lZ < mLength; lZ++)
{
lData.mCellType[lX,lY,lZ] = (int)mCell[lX,lY,lZ].mType;
}
}
}
string lDataString = LitJson.JsonMapper.ToJson(lData);
cFileUtils.WriteStringToFile("TestCluster", lDataString);
Debug.Log("Done saving");
}
,並加載它回來,重新進行功能:
public void LoadCluster()
{
string lDataString = cFileUtils.LoadStringFromFile("TestCluster");
cClusterData lData = new cClusterData();
lData = LitJson.JsonMapper.ToObject<cClusterData>(lDataString);
Debug.Log("Loaded header " + lDataString);
// convert cluster data to actual cluster
mWidth = lData.mWidth;
mHeight = lData.mHeight;
mLength = lData.mLength;
CreateBlankCluster();
for (int lX = 0; lX < mWidth; lX++)
{
for (int lY = 0; lY < mHeight; lY++)
{
for (int lZ = 0; lZ < mLength; lZ++)
{
mCell[lX,lY,lZ].SetType((cCell.eCellType)lData.mCellType[lX,lY,lZ]);
}
}
}
}
一切都很好,直到它試圖訪問lData.mCellType,在此時拋出一個NullReferenceException:
的NullReferenceException:對象沒有設置爲一個對象 (包裝器管理對管理的)對象的實例:ElementAddr(對象,INT,INT,INT)
我的猜測將涉及的方式的陣列是在構造函數中設置,我只是在某處丟失了一行。但我無法解決這個問題。帶上它,互聯網!