我正在使用ASP.NET Forms应用程序,为了简单起见,我添加了模型属性[TotalService]
& [PurchasedorReinstatedService]
我将来自另一个Model的值存储到此属性中,以便在第一个Model中使用。
然后我尝试引用此Model属性并为其设置ASP Label .Text
属性。问题是,我一直得到NullReferenceExceptions,特别是:
An exception of type 'System.NullReferenceException' occurred in PROJECT.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object.
这是我的自定义控件,我收到错误:
using PROJECT.Models.Default;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PROJECT.Classes;
namespace PROJECT.CustomControls.Default
{
public partial class YearsOfService : System.Web.UI.UserControl
{
public List<Years_Of_Service> yos { get; set; }
public Years_Of_Service_Data yosd { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ApplyData();
}
}
private void ApplyData()
{
salaryGridView.DataSource = yos;
salaryGridView.DataBind();
if (yosd.YearsOfService.OrderByDescending(y => y.TotalService).FirstOrDefault().ToString() != null)
{
creditSalLabel.Text = yosd.YearsOfService.OrderByDescending(y => y.TotalService).FirstOrDefault().ToString();
}
else
{
creditSalLabel.Text = String.Empty;
}
//creditSalLabel.Text = yosd.YearsOfService.OrderByDescending(y => y.TotalService).FirstOrDefault() != null ? yosd.YearsOfService.OrderByDescending(y => y.TotalService).FirstOrDefault().ToString() : String.Empty;
//purchaseCreditLabel.Text = yosd.YearsOfService.OrderByDescending(y => y.PurchasedorReinstatedService).FirstOrDefault() != null ? yosd.YearsOfService.OrderByDescending(y => y.PurchasedorReinstatedService).FirstOrDefault().ToString() : string.Empty;
}
}
}
这是我的Years_Of_Service
型号:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace PROJECT.Models.Default
{
public class Years_Of_Service
{
public int Year { get; set; }
public string FiscalYear
{
get
{
return Year + "-" + (Year + 1);
}
}
public decimal ServiceCredited { get; set; }
public decimal Salary { get; set; }
public string CoveredEmployer { get; set; }
// New properties added for Label Values
public string TotalService { get; set; }
public string PurchasedorReinstatedService { get; set; }
}
}
Years_Of_Service_Data
型号:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace PROJECT.Models.Default
{
public class Years_Of_Service_Data
{
public List<Years_Of_Service> YearsOfService { get; set; }
}
}
这是我的Helper中的一些代码,我将数据库结果(Oracle Data Reader
)读入我的Model实体(我已确认这些值已正确添加到TotalService
&amp; ; PurchasedorReinstatedService
属性):
while (odr.Read())
{
Years_Of_Service yosRecord = new Years_Of_Service();
yosRecord.Year = gh.GetSafeInt(odr, 0);
yosRecord.ServiceCredited = gh.ConvertNullDecimal(odr.GetValue(1));
yosRecord.Salary = gh.ConvertNullDecimal(odr.GetValue(2));
yosRecord.CoveredEmployer = gh.GetSafeString(odr, 3);
// Use the Default Info Model Class - At_A_Glance Model properties to fill these properties for text labels.
yosRecord.TotalService = defInfoOasis.At_A_Glance.CreditTotal.ToString("N5");
yosRecord.PurchasedorReinstatedService = defInfoOasis.At_A_Glance.TotalPurchase.ToString("N5");
yos.Add(yosRecord);
}
我现在需要的是将YearsOfService.yos
个实体添加到List<Years_Of_Service> yosd
列表中。有人对如何解决这个问题有任何想法吗?
以下是我在CustomControls中设置get / set下列出的模型的方法:
private void GetDefaultInfoOasis()
{
DefaultInformation dio = new DefaultInformation);
DefaultHelper dh = new DefaultHelper(Convert.ToInt32(Session["memberAccountId"]));
dio = dh.GetDefaultInformation();
// sets the Model Properties on the Custom Controls to that returned by [dio]
AtAGlance.aag = dio.At_A_Glance;
AnnualSalary.a_s = dio.Annual_Salary;
YearsOfService.yos = dio.Years_Of_Service;
// Attempting to add all or even 1 of the [dio.Years_Of_Service] or [YearsOfService.yos] (same thing) to the [YearsOfService.yosd] list:
// Attempt 2: NULL Reference Exception
YearsOfService.yosd.YearsOfService.Add(YearsOfService.yos.First());
// Attempt 1: NULL Reference Exception
YearsOfService.yosd.YearsOfService.Add(new Years_Of_Service { Salary = dio.Years_Of_Service.First().Salary, Year = dio.Years_Of_Service.First().Year, ServiceCredited = dio.Years_Of_Service.First().ServiceCredited, CoveredEmployer = dio.Years_Of_Service.First().CoveredEmployer, TotalService = dio.Years_Of_Service.First().TotalService, PurchasedorReinstatedService = dio.Years_Of_Service.First().PurchasedorReinstatedService });
BeneficiaryDesignation.lb = dio.Beneficiaries;
}
任何人都知道我错过了什么/做错了什么?这可能是获得我想要的长期迂回方式(目前将[TotalService]和[PurchasedorReinstatedService]的相同值添加到我的所有[Years_Of_Service]记录中),但这是我能想到的最简单的方法(不使用全局变量)在我的助手......)
答案 0 :(得分:0)
我没有尝试将List<Years_Of_Service>.First()
插入Years_Of_Service_Data
,而是为我的自定义控件创建了一个常规Year_Of_Service
实体,以便访问:
namespace PROJECT.CustomControls.Default
{
public partial class YearsOfService : System.Web.UI.UserControl
{
public List<Years_Of_Service> lyos { get; set; }
public Years_Of_Service y_o_s { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ApplyData();
}
}
private void ApplyData()
{
salaryGridView.DataSource = lyos;
salaryGridView.DataBind();
if (y_o_s.TotalService.ToString() != null)
{
creditSalLabel.Text = y_o_s.TotalService.ToString();
}
else
{
creditSalLabel.Text = String.Empty;
}
if (y_o_s.PurchasedorReinstatedService.ToString() != null)
{
purchaseCreditLabel.Text = y_o_s.PurchasedorReinstatedService.ToString();
}
else
{
purchaseCreditLabel.Text = String.Empty;
}
}
}
}
我使用自定义控件的默认页面:
private void GetDefaultInformation()
{
DefaultInformation dio = new DefaultInformation();
DefaultHelper dh = new DefaultHelper(Convert.ToInt32(Session["memberAccountId"]));
dio = dh.GetDefaultInformation();
AtAGlance.aag = dio.At_A_Glance;
AnnualSalary.a_s = dio.Annual_Salary;
YearsOfService.lyos = dio.Years_Of_Service;
YearsOfService.y_o_s = dio.Years_Of_Service.First();
}
答案 1 :(得分:0)
我不知道你在哪里初始化YearsOfService.yosd
如果在执行此YearsOfService.yosd.YearsOfService.Add(...)
之前该属性未设置为新对象,那么它将失败。