未处理Nullreference异常(对象引用未设置为对象的实例)

本文关键字:对象 实例 设置 Nullreference 异常 对象引用 未处理 | 更新日期: 2023-09-27 18:08:56

我有一个表单有四个组合说cbmefrom(age from) cbmeto(age to) cbperiod(周期类型值像这个"Next 7 Days" ......)和cbgender(cbgender)..

我也有一个数据网格视图(dgvreports)..

我要做的是填充年龄在20到40岁之间的成员的详细信息,这些成员的会员资格将在未来8天或24天或类似的时间内到期。

为此我编写了一个类,我在下面指定了…

 public static string ConvertGender(string Gender)
 {
     switch (Gender)
     {
         case "Male": return "M";
         case "Female": return "F";
         default: return "";
     }
 }
public BindingSource getmebershipexpirymembers(string gender , DateTime strtdate,DateTime enddate,DateTime min , DateTime max)
{
    bs2.DataSource = null;
    var membersreports = from report in eclipse.members
                        let dob= eclipse.members.Take(1).Select(x=>report.member_Dob).Cast<DateTime>().FirstOrDefault()
                        let strtdatees = eclipse.membertomships.Take(1).Select(x=>x.memberToMship_EndDate).Cast<DateTime>().FirstOrDefault()
                        join memtomship in eclipse.membertomships on report.member_Id equals memtomship.member_Id
                        into joinmemtomship from memtomship in joinmemtomship.DefaultIfEmpty()
                        join mshoption in eclipse.mshipoptions on memtomship.mshipOption_Id equals mshoption.mshipOption_Id
                        into joinmshipoption from mshoption in joinmshipoption.DefaultIfEmpty()
                        join membershiptypes in eclipse.mshiptypes on mshoption.mshipType_Id equals membershiptypes.mshipType_Id
                        into joinmembershipdifftypes from membershiptypes in joinmembershipdifftypes.DefaultIfEmpty()
                        join membershipstatustypes in eclipse.mshipstatustypes on memtomship.mshipStatusType_Id equals membershipstatustypes.mshipStatusType_Id
                        into joinmemberstatusdifftypes from membershipstatustypes in joinmemberstatusdifftypes.DefaultIfEmpty()                                
                        where (report.member_Gender.StartsWith(gender) || string.IsNullOrEmpty(gender))
                        && dob >= min && dob < max
                        && (strtdatees > strtdate && strtdatees < enddate)
                        select new
                        {
                            MemberID = report.member_Id,
                            Lastname = report.member_Lastname,
                            Firstname = report.member_Firstname,
                            Postcode = report.member_Postcode,
                            Reference = report.member_Reference,
                            CardNum = report.member_CardNum,
                            IsBiometric = report.member_IsBiometric,
                            DOB = report.member_Dob,
                            MShipType = membershiptypes.mshipType_Name,
                            StatusType = membershipstatustypes.mshipStatusType_Name,
                            EndDate = memtomship.memberToMship_EndDate
                        };
        bs2.DataSource = membersreports;
    return bs2;
 }

,我以下面提到的形式访问上面的类…

  public void Getgroupcorporatemembers()
  {
        int startdays = 0;
        int enddays = 0;
        if (cbMeperiodType.Text == membershipexpiry.type1)
        {
            startdays = 0;
            enddays = 7;
        }
        if (cbMeperiodType.Text == membershipexpiry.type2)
        {
            startdays = 8;
            enddays = 14;
        }
        if (cbMeperiodType.Text == membershipexpiry.type3)
        {
            startdays = 15;
            enddays = 30;
        }
        if (cbMeperiodType.Text == membershipexpiry.type4)
        {
            startdays = 31;
            enddays = 90;            
        }
        DateTime today = DateTime.Now;
        DateTime strtdate = today.AddDays(startdays);
        DateTime enddate = today.AddDays(enddays);
        int agefrom = Convert.ToInt32(cbMeFrom.Text);
        int ageto = Convert.ToInt32(CbMeTo.Text);
        DateTime max = today.AddYears(-agefrom);
        DateTime min = today.AddYears(-ageto);
        string gender = "";
        gender = Classes.reportmembers.ConvertGender(cbMEGendertype.Text);
            dgvReportMembers.DataSource = objreports.getmebershipexpirymembers(gender, strtdate, enddate, max, min);// here i am accessing the method in class
            SetDgvheaders();

    }
    struct membershipexpiry
    {
        public const string type1 = "Next 7 Days";
        public const string type2 = "8 - 14 Days";
        public const string type3 = "15 - 30 Days";
        public const string type4 = "31 - 90 Days";        
    }

但是我在这一行得到错误…bs2.DataSource = membersreports;

   Error :Nullreference excpetion was unhandled 
          Object reference not set to an instance of an object.

未处理Nullreference异常(对象引用未设置为对象的实例)

我同意Yochai的观点,最有可能的问题是查询中的let语句。例如,如果member_Dob为空,则Cast方法将抛出NullReferenceException。

Linq查询在被枚举之前不会被执行。数据源赋值语句将枚举查询,但它不会告诉您查询在哪一行失败。尝试手动枚举查询,使用Try catch块并在catch块中设置断点。这可以帮助您找到引起问题的行。

        foreach (var memb in membersreports)
        {
            try
            {
                Console.WriteLine(memb.DOB);
            }
            catch (Exception ex)
            {
                //set a breakpoint here or in the try block
                Console.WriteLine(ex.Message);
            }
        }

这可能是查询,而不是我最初认为的bs2变量。

字段(如eclipse.mshipoption)不会引发异常。

Take(1)可能得不到结果,那么当您尝试强制转换结果时,它将失败。

我试着看看这些返回什么:

let dob = eclipse.members.Take(1)
                         .Select(x => report.member_Dob)
                         .Cast<DateTime>()
                         .FirstOrDefault()
let strtdatees = eclipse.membertomships.Take(1)
                                       .Select(x => x.memberToMship_EndDate)
                                       .Cast<DateTime>()
                                       .FirstOrDefault()