0

An error 'object reference not set to an instance of an object' occurs when I'm using the class object cleave below the for loop

private void dateTimePickertodate_ValueChanged(object sender, EventArgs e) { if (dateTimePickertodate.Value <=dateTimePickerfromdate.Value) { MessageBox.Show("Choose Correct date"); textBoxnumofdays.Clear(); } else { cleave = new LeaveApplication(constr); span = dateTimePickertodate.Value - dateTimePickerfromdate.Value; Getdays(); if (Mode == 1) { textBoxnumofdays.Text = Convert.ToString(span.Days + 2); } else { textBoxnumofdays.Text = Convert.ToString(span.Days + 1); } for (int i = 0; i < daysofweek.Count; i++) { if (Mode == 1) { textBoxnumofdays.Text = Convert.ToString(span.Days + 2); if (daysofweek[i].Equals(cleave.WeekDays[i])) { textBoxnumofdays.Text = Convert.ToString(span.Days - 1); } } else { textBoxnumofdays.Text = Convert.ToString(span.Days + 1); if (daysofweek[i].Equals(cleave.WeekDays[i])) { textBoxnumofdays.Text = Convert.ToString(span.Days - 1); } } } } } 
5
  • 5
    You should look at debugging this yourself first - for example, in the IDE what line does the exception happen on? And what are the variables at that point? There is a lot you can do to diagnose this yourself... Commented Feb 14, 2011 at 10:13
  • Or at least post the exact error, with line number, and method call that triggers this error. Have you tried anything? Commented Feb 14, 2011 at 10:14
  • cleave.WeekDays has null value..but it has value in its class..how can i access that value into the code of the form Commented Feb 14, 2011 at 10:17
  • in 3 tier architecture how can i access the form in the class..is it possible??if so plz help me..if i use a list in form and if i add weekdays elements to this list i think weekdays wont be null..how can i do that? Commented Feb 14, 2011 at 10:26
  • 3 tier architecture has nothing at all to do with this... that is like saying "in 3 tier architecture, how can I concatenate two strings". What is it you actually want to do here? Commented Feb 14, 2011 at 10:32

3 Answers 3

2

when iam using the class object cleave below the for loop

you don't show anything below the for loop, and cleave isn't declared in the method - so we infer that it is a field. We can therefore assume one of two things:

  • the date selected wasn't a valid date, so cleave was never assigned new LeaveApplication(constr);
  • the event hasn't fired, dateTimePickertodate_ValueChanged has never been invoked - so again, cleave hasn't been assigned
Sign up to request clarification or add additional context in comments.

1 Comment

in 3 tier architecture how can i access the form in the class..is it possible??if so plz help me..if i use a list in form and if i add weekdays elements to this list i think weekdays wont be null..how can i do that?
1

Are you sure that the WeekDays array is initialized in your LeaveApplication constructor. If not, it will throw a NullReferenceException in your statement:

if (daysofweek[i].Equals(cleave.WeekDays[i]))

Comments

0

Simply put an instance of cleave hasn't been created by the time that you wish to use it.

Have you checked that it isn't null and that a non-null instance of cleave has been created?

One of the problems here is that we have to assume that cleave, constr and span are fields within your "form" and that they are of the correct type definition and that they are accessible and valid for the thread that your code is running on.

edit

Seeing more of your comments. If any of the data used on your form is pushed by code that is not running on the same thread as your GUI rather than retrieved by the form class then you'll need to define a delegate on your form that can be used by that code to update the data. By using "form class".Invoke(delegate function) type functionality.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.