Skip to content

Object reference not set to an instance of an object err

Pelumi Eluyera

Asked Mar 23, 2015, 6:15 AM EDT3 answers
Share
LinkedInX
Consider the following code.

C#
private DataSetNumber1.TBL_AppointmentRow _selectedAppointmentRow = null ; private DatasetNumber1.TBL_EngineerRow _selectedEngineerRow = null;  public myEditSchedulerDialog () {  initializeComponent (); _selectedAppointmentRow = new DataSetNumber1.TBL_AppointmentRow (null);  }

I keep getting a object reference not set to an instance of an object error.


Any help would be appreciated.

regards



3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Afzaal Ahmad Zeeshan
    +1
    What else would you expect from above code then?


    Please, initialize the variables which have been given a value of null; null means nothing, it doesn't exist. That is why you're getting this error. Once your object would have some value, it would not throw similar exception again. You can use new Constructor() to instantiate the object. 
    +1
  • Afzaal Ahmad Zeeshan
    0
    Did this object still throw error for being a null variable? 


    Because, using new keyword and creating the object would remove any chances for getting a null exception, until an underlying object is null. Please use breakpoints in your code and debug it, going through every statement in your code to see which statement, expression or variable throws exception, then try to instantiate it to fix this problem. It is hard to tell without having to look at the code of your project or how you use them in your application.
    0
  • -1
    could you please give me an example?


    I have tried using
    _selecdted  AppointmentRow = new DataSetNumber1.Tbl_AppointmentRow ();

    Regards
    -1