如何在我的代码中修复未设置为对象实例的对象引用。 [英] How do I fix object reference not set to instance of object in my code.
本文介绍了如何在我的代码中修复未设置为对象实例的对象引用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨。
我在我的应用程序中使用以下代码,但我无法解决。任何人帮助我PLZ。
我的课程代码是
命名空间 Semantics3API
{
public class 优惠
{
public string SellerName { get ; set ; }
public string 价格{ get 跨度>; set ; }
public string 货币{ get 跨度>; set ; }
public string 可用性{ get 跨度>; set ; }
}
//
public class 商店
{
public < span class =code-keyword> string SkuInStore { get ; set ; }
public string ProductUrlInStore { get 跨度>; set ; }
public 列表<优惠>优惠{获取; set ; }
}
//
public class 图片
{
public < span class =code-keyword> string imageUrl;
}
//
公开 类产品
{
public string 标题{ get ; set ; }
public 列表<图像>图片{获取; set ; }
public 列表<商店>商店{获取; set ; }
}
}
这里是错误的空引用
商店store = new 商店();
store.SkuInStore = skuInStore;
store.ProductUrlInStore = productUrlInStore;
优惠商品= 新商品();
offer.SellerName = sellerName;
offer.Price = price;
offer.Currency = currency;
offer.Availability = availability;
// 向商店添加商品
store.Offers.Add(offer) ; // 对象引用未设置为对象错误的实例:(
解决方案
public 类商店
{
public 商店()
{
商品= new 列表< Offers>();
}
public string SkuInStore { get ; set ;}
public string ProductUrlInStore { get ; set ;}
public 列表<优惠>优惠{获取; set ;}
}
Hi.
I have following code to use in my application but I am unable to solve. Any one help me plz.
My code for class is
namespace Semantics3API { public class Offers { public string SellerName { get; set; } public string Price { get; set; } public string Currency { get; set; } public string Availability { get; set; } } // public class Stores { public string SkuInStore { get; set; } public string ProductUrlInStore { get; set; } public List<Offers> Offers { get; set; } } // public class Images { public string imageUrl; } // public class Product { public string Title { get; set; } public List<Images> Images { get; set; } public List<Stores> Stores { get; set; } } }
And here is the error null reference
Stores store = new Stores(); store.SkuInStore = skuInStore; store.ProductUrlInStore = productUrlInStore; Offers offer = new Offers(); offer.SellerName = sellerName; offer.Price = price; offer.Currency = currency; offer.Availability = availability; //adding offer to store store.Offers.Add(offer); // object reference not set to instance of object error :(解决方案public class Stores { public Stores() { Offers = new List<Offers>(); } public string SkuInStore { get; set; } public string ProductUrlInStore { get; set; } public List<Offers> Offers { get; set; } }
这篇关于如何在我的代码中修复未设置为对象实例的对象引用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文