开发者

Nullreference excpetion was unhandled (Object reference not set to an instance of an object)

开发者 https://www.devze.com 2023-04-04 23:29 出处:网络
I have a form with four combobxes say cbmefrom(age from ) cbmeto( age to)cbperiod(periodtype values liek this\"Next 7 Days\" ......) and cbgender(cbgender)..

I have a form with four combobxes say cbmefrom(age from ) cbmeto( age to) cbperiod(periodtype values liek this "Next 7 Days" ......) and cbgender(cbgender)..

i have a datagrid view also(dgvreports)..

what i am trying to do is i am populating the members details those whoose age is between 20 to 40 and whoose membership will expire in next 8 days or 24 days or like that ...

for that i have written one class that i have specified below...

 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;
 }

and i am accessing above class in the form that i have mentioned below...

  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 st开发者_开发问答ring 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";        
    }

but I am getting the error at this line ... bs2.DataSource = membersreports;

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


I agree with Yochai, the most likely problem is the let statements in the query. For instance, if there is a null member_Dob, then the Cast method will throw a NullReferenceException.

A Linq query is not executed until it is enumerated. The datasource assignement statement will enumerate the query, but it's not telling you on what row it failed. Try to enumerate the query manually, with a try catch block and set a breakpoint in the catch block. That may help you to find the row that is causing the problem.

        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);
            }
        }


It's probably the query, not the bs2 variable as I initially thought.

The fields (like eclipse.mshipoption) wont raise an exception.

The Take(1) might not get a result, then when you try to cast the result it will fail.

I'd try to see what these return:

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()
0

精彩评论

暂无评论...
验证码 换一张
取 消