You must Sign In to post a response.
  • Category: ASP.NET

    Object reference not set to an instance of an object

    In the production environment of an website,sometimes error coming as
    Object reference not set to an instance of an object or some variable not declared

    or sometimes partially saved.I am unable to debug it. Most of the time the code is working fine.

    Please suggest a solution.
  • #767356

    Refer below..
    In your dev environment you are unable to get the issue repro
    And in Prod environment if it is coming, just observe what are the actions performed at that time(use event logs when that error u got).
    Try to find the scenario and debug the same scenario in dev environment.
    Also if you miss try catch block in that scenario action code try to include that.

    Never lose hope..You never know what tomorrow will bring

  • #767358
    You can check servers event log as follows:
    Administrative Tools--> Event Viewer--> Windows Logs--> Application
    Inside that please checl 'Error' levels where you may find details regarding that 'Object reference error' like error page\error line number. From this you can trace out exact reason which causing this error.

  • #767361

    In your local you didn't get this problem right? Only in production data only you got this issue right? In that case my suggestion is get the production data and restore it in your local then try to reproduce the same error and then check which line you got this error there you need to check that whether the object you are accessing is valid one or not. i.e. as per error details if you are trying to assign null object or you are trying to accessing null object that time you got this type of error, be ensure before accessing such type of data you must and should check null condition before accessing that type of object.

    Give respect to your work, Instead of trying to impress your boss.

    Blog :

  • #767389
    compiler throws "object reference" error when you have try to fetch some value from object which is null, so you need to use TRY...CATCH in this case, just catch the exception and write stacktrace in code so that any code easily track out and you can find where the exact error caught.
    do it with following code standard

    // i.e When the user clicks on a button
    catch(Exception ex)
    ex.Log(); // Log exception

    -- OR --

    ex.Log().Display(); // Log exception, then show it to the user with apologies...

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #767413
    Are you using any session in your page..?
    if you are using session it does not pass value one page to another page.
    in your session read page you may type wrong of your session value .
    it may reason for your problem.
    first put a break point check lines one by one


  • #767415
    The exception clearly showing the there is an error to an object. It may be because of the following reason.
    You can assigning the value of the property without creating the instance.
    For ex.

    Customer MyCustomer = new Customer();
    Address MyAddressL = new Address();

    In the above case you should create instance for MyCustomer.Address = new List<Address>()
    In your code some cases you may miss to handle some logic. Try to check are you creating instances proper.

    I think its production issue. So try to handle, and get proper error and error line. So you can fix the issue quickly

    < customErrors
    / >

    By Nathan
    Direction is important than speed

  • #767423
    Hai Sweety K Paul,
    There could be various reasons due to which you are getting this error. To find out where you got this exact error, you can get access to production machine and go to the Event viewer for that machine.
    In the event viewer, you can find your application error logs which will get generated when the error comes.
    So get the detailed issue from the event viewer and do the work around for it.
    There could be some circumstances where your data is invalid and causing this type of error.
    Hope it will be helpful to you.

    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)

  • #767472
    Yes I am using session variable. I am accessing the session variable and trying to save it.Only very rarely this problem occurs. But it should be resolved.

    Dim dT As DataTable

    dT = session("CurrentTable")
    I tried to save the values from the datatable. some times it will show that dT not declared.

Sign In to post your comments