问题描述
- 报错:未将对象引用设置到对象的实例。下面是代码
-
public class Temporary
{
//public string Query_NAME { get; set; }
public long Te_TC { get; set; }
public long Te_MC { get; set; }
public long Te_QC { get; set; }
public long Te_WC { get; set; }
public long Te_CC { get; set; }
public long Te_EC { get; set; }
public long Te_PC { get; set; }
}
private DataServiceCollection temporary;public DataServiceCollection<Temporary> AllTemporary { get { return temporary; } set { if (temporary != value) { temporary = value; this.OnPropertyChanged("AllTemporary"); } } } public object QueryInventoryInfo(string areaId) { if (string.IsNullOrEmpty(areaId)) { areaId = ""; } string locType = "2"; for (int i = 0; i < Areas.Count; i++) { Temporary tem = new Temporary(); tem.Te_TC = this.WMSServiceContext.MD_LOCATION.Where(c => c.LOC_TYPE != locType && c.AREA_ID.StartsWith(areaId)).LongCount(); tem.Te_MC = this.WMSServiceContext.MD_LOCATION.Where(c => c.LOC_TYPE != locType && c.AREA_ID.StartsWith(areaId) && c.STATUS.Contains("m")).LongCount(); tem.Te_QC = this.WMSServiceContext.MD_LOCATION.Where(c => c.LOC_TYPE != locType && c.AREA_ID.StartsWith(areaId) && c.STATUS.Contains("q")).LongCount(); tem.Te_WC = this.WMSServiceContext.MD_LOCATION.Where(c => c.LOC_TYPE != locType && c.AREA_ID.StartsWith(areaId) && c.STATUS.Contains("w")).LongCount(); tem.Te_CC = this.WMSServiceContext.MD_LOCATION.Where(c => c.LOC_TYPE != locType && c.AREA_ID.StartsWith(areaId) && c.STATUS.Contains("c")).LongCount(); tem.Te_EC = this.WMSServiceContext.MD_LOCATION.Where(c => c.LOC_TYPE != locType && c.AREA_ID.StartsWith(areaId) && c.STATUS.Contains("e")).LongCount(); tem.Te_PC = this.WMSServiceContext.MD_LOCATION.Where(c => c.LOC_TYPE != locType && c.AREA_ID.StartsWith(areaId) && c.STATUS.Contains("p")).LongCount(); if (tem != null) { this.AllTemporary.Add(tem); //此处报错 } } return AllTemporary; }
解决方案
错在哪一行就检查那一行上的几个变量,看哪个是null
解决方案二:
AllTemporary没初始化?
解决方案三:
你应该先判断new 成功没有,new 返回的是指针,你的好像不是Temporary tem = new Temporary();-->
Temporary *tem = new Temporary();??
时间: 2025-01-30 00:49:15