I'm working with an ASP.NET Forms application where (for simplicity) I added Model properties [TotalService]
& [PurchasedorReinstatedService]
where I am storing the value from another Model into this property for use in the first Model.
I am then trying to reference this Model property and set an ASP Label .Text
property to it. Problem is, I keep getting NullReferenceExceptions, specifically:
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.
This is my custom control where I'm getting the error:
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();
decimal totalService = 0;
foreach (GridViewRow row in salaryGridView.Rows)
{
totalService += Convert.ToDecimal(row.Cells[1].Text);
}
if (yosd.YearsOfService.OrderByDescending(y => y.TotalService).FirstOrDefault().ToString() != null) // NULL REFERENCE HERE
{
creditSalLabel.Text = yosd.YearsOfService.OrderByDescending(y => y.TotalService).FirstOrDefault().ToString();
}
else
{
creditSalLabel.Text = String.Empty;
}
}
}
}
This is my Years_Of_Service
model:
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
Model:
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; }
}
}
and this is the bit of code in my Helper where I am reading from my database results ( Oracle Data Reader
) into my Model entity (I have confirmed the values are getting correctly added to the TotalService
& PurchasedorReinstatedService
properties):
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);
}
Originally I tried the following, also getting the FirstOrDefault() entity, with the same results of NullReferenceException
:
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;
Anyone have any ideas for how I can set my Label.Text
property to my (any entity) Model properties [TotalService]
& [PurchasedorReinstatedService]
? With the way I'm setting the property, each entity in the referenced model classes will have the same value for these 2 properties.
My guess is you are getting back the default which is null
yosd.YearsOfService.OrderByDescending(y => y.TotalService).FirstOrDefault()
Then you are doing the ToString() on the null.
If that is not it, I would look at yosd for null or yosd.YearsOfService for null.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.