Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在2048游戏中反序列化JSON中的值数组会导致NullReferenceException_C#_Json_Serialization_Windows Phone 8_Deserialization - Fatal编程技术网

C# 在2048游戏中反序列化JSON中的值数组会导致NullReferenceException

C# 在2048游戏中反序列化JSON中的值数组会导致NullReferenceException,c#,json,serialization,windows-phone-8,deserialization,C#,Json,Serialization,Windows Phone 8,Deserialization,我正在为我的大学课程制作一个2048克隆。我有一个自定义类: public class Field { public int value; public bool stop; } 以及这些字段的数组: public Field[,] gameArray = new Field[4, 4]; 它们在自定义类中。 以下是我用于在item page cs文件中保存和加载此数组的方法: private async void saveAsync() {

我正在为我的大学课程制作一个2048克隆。我有一个自定义类:

public class Field
{
    public int value;
    public bool stop;
}
以及这些字段的数组:

public Field[,] gameArray = new Field[4, 4];
它们在自定义类中。 以下是我用于在item page cs文件中保存和加载此数组的方法:

    private async void saveAsync()
    {
        string jsonContents = JsonConvert.SerializeObject(currentGame.gameArray);
        StorageFolder localFolder = ApplicationData.Current.LocalFolder;
        StorageFile textFile = await localFolder.CreateFileAsync(saveFileName,
                                     CreationCollisionOption.ReplaceExisting);
        using (IRandomAccessStream textStream = await textFile.OpenAsync(FileAccessMode.ReadWrite))
        {
            using (DataWriter textWriter = new DataWriter(textStream))
            {
                textWriter.WriteString(jsonContents);
                await textWriter.StoreAsync();
            }
        }
    }
    private async void loadAsync()
    {
        StorageFolder localFolder = ApplicationData.Current.LocalFolder;
        StorageFile textFile = await localFolder.GetFileAsync(saveFileName);
        using (IRandomAccessStream textStream = await textFile.OpenReadAsync())
        {
            using (DataReader textReader = new DataReader(textStream))
            {
                if ((uint)textStream.Size != 0)
                {
                    uint textLength = (uint)textStream.Size;
                    await textReader.LoadAsync(textLength);
                    string jsonContents = textReader.ReadString(textLength);
                    currentGame.gameArray = JsonConvert.DeserializeObject<Field[,]>(jsonContents);
                }
                else currentGame.Reset();
            }
            update();
        }
    }

第一行给了我一个NullReferenceException。我可能把数组的序列化和反序列化搞砸了,但我根本不知道它是什么,也不知道如何修复它,这是我第一次尝试序列化,我基本上是从互联网上复制的。我是否需要传递对loadAsync()的引用;方法?

看起来反序列化没有正确进行。currentGame、gameArray或gameArray[0,0]为空。你需要调试代码,找出这三个文件中的哪一个是空的。我有一个if语句,检查json文件是否为空,如果为空,它会重置棋盘,并且工作正常,因此currentGame和gameArray都不为空,据我所知,导致bug的必须是我正在反序列化的对象。currentGame是从json反序列化中初始化的。如果反序列化没有正确进行,那么它将为null。您需要检查json值。您能否共享当前游戏类结构和从中反序列化的json?
txt10.Text = currentGame.gameArray[0, 0].value.ToString();
color(b10, currentGame.gameArray[0, 0].value);