XML 序列化程序导致 Photon RPC 命令中的 nullreference

xml serializer causes nullreference in photon rpc comand

提问人:Eduard Hasanaj 提问时间:8/14/2015 最后编辑:Eduard Hasanaj 更新时间:8/14/2015 访问量:60

问:

我得到了这个代码 xml 序列化程序并试图让它工作,但创建的对象包含 nullreference

public  byte[] SerializeObject <T>(T serializableObject)
{
    T obj = serializableObject;

    using (MemoryStream stream = new MemoryStream())
    {
        System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(T));

        x.Serialize (stream,obj);

        return stream.ToArray ();
    }
}

public  T DeserializeObject <T>(byte[] serilizedBytes)
{
    System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer (typeof(T));

    using (MemoryStream stream = new MemoryStream(serilizedBytes)) 
    {
        return (T) x.Deserialize (stream);
    } 
}

我使用此函数初始化类变量,但找不到 null 在哪里

public void updateinfo(int []card, int lmin, int lbom, int pass, int mxint,int cardinturn, int lastsim, int fplayer, float ang, int playid,string namepl,int numberofcard){
    o1=new playerupdates();
    o1.cardstoad = new int[card.Length];
    for(int i=0;i<card.Length;i++){
        o1.cardstoad[i]=card[i];
    }
o1.lastmin=lmin;
o1.lastbomb=lbom;
o1.passnumber=pass;
o1.maxvalueinturn=mxint;
o1.cardsinturn=cardinturn;
o1.lastsimiliarvalue=lastsim;
o1.firstplayer=fplayer;
o1.angle=ang;
o1.playerid=playid; 
o1.playername=namepl;
o1.cardsin=numberofcard;

}

我一直在寻找解决方案,但一无所获。初始化类变量的函数是否正确? 编辑 班级代码

public int []cardstoad { get; set; }
public int lastmin { get; set; }
public int lastbomb { get; set; }
public int passnumber{ get; set; } 
public int maxvalueinturn { get; set; }
public int cardsinturn { get; set; }
public int lastsimiliarvalue { get; set; }
public int firstplayer { get; set; }
public float angle{ get; set; }
public int playerid { get; set; }
public string playername { get; set; }
public int cardsin{ get; set; }
C# XML NullReferenceException 序列化

评论

0赞 Volkan Paksoy 8/14/2015
请发布示例 XML。还有您用于序列化的类(我猜是 playerupdates)
0赞 Eduard Hasanaj 8/14/2015
我添加了类代码。我删除了 cardstoad 和 updateinfo 中的其他变量,我做到了 o1=new playerupdates(){lastmin=lmin, lastbomb=lbom,etc};但什么都没有。Nullreference 静止图像再次出现
0赞 Volkan Paksoy 8/14/2015
此代码中没有任何内容使用 XML 序列化,也没有 XML 示例。
0赞 Eduard Hasanaj 8/14/2015
updateinfo(lastmin,lastbomb,passnumber,maxvalueinturn,cardsinturn,lastsimiliarvalue,firstplayer,angle,playerid,playername,playercardsc);photonView.RPC(“updatecardsintable”,PhotonTargets.All,selem.SerializeObject(o1),Serelemen.cards);塞勒姆。SerializeObject(o1) 包含我给定的 SerializeObject 函数的 nullreference。请参阅下面的代码

答:

0赞 jdweng 8/14/2015 #1

你的问题是 o1 没有做任何事情。UpdateInfo 正在设置值,但 O1 在从 UpdateInfo 返回后丢失。有关更正,请参阅下面的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] card = {1,2,3,4,5};
            int lmin = 4;
            int lbom = 5;
            int pass = 6;
            int mxint = 7;
            int cardinturn = 8;
            int lastsim = 9;
            int fplayer = 10;
            float ang = 30.0F;
            int playid = 100;
            string namepl = "abc";
            int numberofcard = 26;
            playerupdates pU = new playerupdates();
            pU.updateinfo(card, lmin, lbom, pass, mxint, cardinturn, lastsim, fplayer, ang, playid, namepl, numberofcard);
            byte[] data = pU.SerializeObject<playerupdates>(pU);
            string test = Encoding.UTF8.GetString(data);    //test to see if serialize is working correctly
            playerupdates newPU = pU.DeserializeObject<playerupdates>(data);
        }
    }

    [XmlRoot("playerupdates")]
    public class playerupdates
    {
        [XmlElement("cardstoad")]
        public int[] cardstoad { get; set; }
        [XmlElement("lastmin")]
        public int lastmin { get; set; }
        [XmlElement("lastbomb")]
        public int lastbomb { get; set; }
        [XmlElement("passnumber")]
        public int passnumber { get; set; }
        [XmlElement("maxvalueinturn")]
        public int maxvalueinturn { get; set; }
        [XmlElement("cardsinturn")]
        public int cardsinturn { get; set; }
        [XmlElement("lastsimiliarvalue")]
        public int lastsimiliarvalue { get; set; }
        [XmlElement("firstplayer")]
        public int firstplayer { get; set; }
        [XmlElement("angle")]
        public float angle { get; set; }
        [XmlElement("playerid")]
        public int playerid { get; set; }
        [XmlElement("playername")]
        public string playername { get; set; }
        [XmlElement("cardsin")]
        public int cardsin { get; set; }

        public void  updateinfo(int[] card, int lmin, int lbom, int pass, int mxint, int cardinturn, int lastsim, int fplayer, float ang, int playid, string namepl, int numberofcard)
        {
            //o1 not needed
            //playerupdates o1 = new playerupdates();
            cardstoad = new int[card.Length];
            for (int i = 0; i < card.Length; i++)
            {
                cardstoad[i] = card[i];
            }
            lastmin = lmin;
            lastbomb = lbom;
            passnumber = pass;
            maxvalueinturn = mxint;
            cardsinturn = cardinturn;
            lastsimiliarvalue = lastsim;
            firstplayer = fplayer;
            angle = ang;
            playerid = playid;
            playername = namepl;
            cardsin = numberofcard;
        }

        public byte[] SerializeObject<T>(T serializableObject)
        {
            T obj = serializableObject;

            using (MemoryStream stream = new MemoryStream())
            {
                System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(T));

                x.Serialize(stream, obj);

                return stream.ToArray();
            }
        }

        public T DeserializeObject<T>(byte[] serilizedBytes)
        {
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(T));

            using (MemoryStream stream = new MemoryStream(serilizedBytes))
            {
                return (T)x.Deserialize(stream);
            }
        }

    }
}
​

评论

0赞 Eduard Hasanaj 8/14/2015
updateinfo 不在 playerupdates 中,而是在另一个类中,其中 main 函数,这就是 playerupdates o1 = new playerupdates(){} 的原因,但无论如何,我想问一下,当调用函数 serialise 时,这些函数是否被序列化。如果没有,请告诉我
0赞 jdweng 8/14/2015
我发布的代码经过了全面测试。您的问题的答案是否定的。变量 o1 是一个局部变量,该值在执行 updateinfo 后被释放。如果您将 o1 设置为另一个类中的公共变量,那么它将被序列化。
0赞 Eduard Hasanaj 8/15/2015
我赞成你的答案,因为它解决了序列化中的 nullreference,但在反序列化中它显示 nullreference public void updatecardsintable(byte [] crdstotable){ playerupdates o2 = selem。DeserializeObject<playerupdates>(crdstotable);lastmin=o2.lastmin;lastbomb=o2.lastbomb;passnumber=o2.passnumber;maxvalueinturn=o2.maxvalueinturn;cardsinturn=o2.cardsinturn;lastsimiliarvalue=o2.lastsimiliarvalue;firstplayer=o2.firstplayer;角度=o2.角度;玩家 ID = o2.玩家 ID;玩家名称=o2.playername;卡西=o2.卡西;}
0赞 Eduard Hasanaj 8/15/2015
你的答案是正确的解决方案,但问题是光子网络。或者这一行 playerupdates o2 = selem。DeserializeObject<playerupdates>(crdstotable);我做错了什么?
0赞 jdweng 8/15/2015
需要查看 xml。确保 XML 中的标记与 [Xml...] 语句(包括大写/小写)匹配。还要检查数组项,例如 cardstoad。我使用了 XmlElement,它只创建单级标签而不是两层<cardstoad><cardstoad></cardstoad></cardstoad>。还要确保对象是“公共”的。