本文介绍了IList< int> throws Null添加值时引用异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类:
public class ClientModelData
{
public int clientID {get;组; }
public IList< int> LocationIDs {get;组; }
}
当我调用它:
ClientModelData obj = new ClientModelData();
obj.LocationIDs.Add(1);
它会引发异常:
`((System.Collections.Generic.ICollection< int>)(client.LocationID))'is null`
解决方案
LocationIDs
未初始化,因此它给你的错误。public IList< int> LocationIDs {get;组; }
您应该在构造函数中创建一个实例
public ClientModelData()
{
LocationIDs = new List< int>();
}
I have a class:
public class ClientModelData { public int clientID { get; set; } public IList<int> LocationIDs { get; set; } }
When I call it:
ClientModelData obj = new ClientModelData(); obj.LocationIDs.Add(1);
It throws an exception:
`((System.Collections.Generic.ICollection<int>)(client.LocationID))' is null`
解决方案
LocationIDs
is not initialized therefore it is giving you the error.public IList<int> LocationIDs { get; set; }
You should create an instance in the constructor
public ClientModelData() { LocationIDs = new List<int>(); }
这篇关于IList< int> throws Null添加值时引用异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!