创建要序列化为json的对象会抛出nullreferenceexception [英] Creating object to serialize to json throws nullreferenceexception

查看:120
本文介绍了创建要序列化为json的对象会抛出nullreferenceexception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public   class  RootJson 
{
public string App {获得; set ; }
public 列表< SourcesFile> ListSourcesFile { get ; set ; }
}

public class SourcesFile
{
public string 来源{获取; set ; }
}



这是我的根



现在:



 RootJson rootjson =  new  RootJson()
rootjson.App = appIdentifier.AppName;

使用(StreamWriter file = File.CreateText( @ c:\ test\source.json))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file,rootjson);
}





这样也可以。但这不起作用:



 rootjson.ListSourcesFile [ 0  ] .Source =   Fdf; 





它给出了这个错误



对象引用不是设置为对象的实例。



我认为这是因为引用未初始化。但我怎么能这样做?



我尝试过的事情:



ourcesFile ListSourcesFile = new SourcesFile();

解决方案

第一个问题是你没有初始化列表,所以 rootjson.ListSourcesFile 返回 null ,任何索引到列表的尝试都会抛出 NullReferenceException



第二个问题是你没有在列表中添加任何内容。当您尝试检索空列表的第一项时,您将获得 ArgumentOutOfRangeException ,告诉您您尝试访问的索引不存在。



尝试在访问之前初始化列表:

 RootJson rootjson =  new  RootJson 
{
App = appIdentifier.AppName,
ListSourcesFile = new List< SourcesFile>
{
new SourcesFile
{
Source = Fdf
},
},
};

// 或者:
RootJson rootjson = new RootJson();
rootjson.App = appIdentifier.AppName;
rootjson.ListSourcesFile = new List< SourcesFile>();

SourcesFile file = new SourcesFile();
file.Source = Fdf;
rootjson.ListSourcesFile.Add(file);


RootJson rootjson = new RootJson

{

App = appIdentifier.AppName,

ListSourcesFile = new List< sourcesfile>

{

new SourcesFile

{

来源=Fdf,

},

新SourcesFile

{

来源=Sdr,

},

},

};

或:

SourcesFile file = new SourcesFile();

file.Source =Fdf;

rootjson.ListSourcesFile.Add(file);



file = new SourcesFile();

file.Source =Sdr;

rootjson.ListSourcesFile.Add (文件);

public class RootJson
{
    public string App { get; set; }
    public List<SourcesFile> ListSourcesFile { get; set; }
}

public class SourcesFile
{
    public string Source { get; set; }
}


This is my root

Now:

RootJson rootjson = new RootJson()
rootjson.App = appIdentifier.AppName;

using (StreamWriter file = File.CreateText(@"c:\test\source.json"))
{
   JsonSerializer serializer = new JsonSerializer();
   serializer.Serialize(file, rootjson);
}



This works fine too. But this doesn't work:

rootjson.ListSourcesFile[0].Source = "Fdf";



It gives this error

Object reference not set to an instance of an object.

I think it's because the reference is not initialized. But how can i do this?

What I have tried:

ourcesFile ListSourcesFile = new SourcesFile ();

解决方案

The first problem is that you haven't initialized the list, so rootjson.ListSourcesFile returns null, and any attempt to index into the list throws a NullReferenceException.

The second problem is that you haven't added anything to the list. When you try to retrieve the first item of an empty list, you'll get an ArgumentOutOfRangeException telling you that the index you're trying to access does not exist.

Try initializing the list before you access it:

RootJson rootjson = new RootJson
{
    App = appIdentifier.AppName,
    ListSourcesFile = new List<SourcesFile>
    {
        new SourcesFile
        {
            Source = "Fdf",
        },
    },
};

// Or:
RootJson rootjson = new RootJson();
rootjson.App = appIdentifier.AppName;
rootjson.ListSourcesFile = new List<SourcesFile>();

SourcesFile file = new SourcesFile();
file.Source = "Fdf";
rootjson.ListSourcesFile.Add(file);


RootJson rootjson = new RootJson
{
App = appIdentifier.AppName,
ListSourcesFile = new List<sourcesfile>
{
new SourcesFile
{
Source = "Fdf",
},
new SourcesFile
{
Source = "Sdr",
},
},
};
or:
SourcesFile file = new SourcesFile();
file.Source = "Fdf";
rootjson.ListSourcesFile.Add(file);

file = new SourcesFile();
file.Source = "Sdr";
rootjson.ListSourcesFile.Add(file);


这篇关于创建要序列化为json的对象会抛出nullreferenceexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆