Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hii,
I have a problem my application works fine on local machine but on IIS server .Don't know what to do its works fine on local machine.it show some Exception like:-

"Object reference not set to an instance of an object."

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 61: }
Line 62: }
Line 63: if (selectAllFile.Checked == true)
Line 64: {}
Line 65: else{

Source File: E:\ConfidentPDR\ConfidentPDR(2 Sept 2014)\ConfidentPDR(19 Aug)\ManuHelp.ConfidentPDR.Presentation.MVC\Appointments\FileEntryManagement.aspx.cs Line: 63

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
ManuHelp.ConfidentPDR.Presentation.MVC.Appointments.FileEntryManagement.OnPreRender(EventArgs e) in E:\ConfidentPDR\ConfidentPDR(2 Sept 2014)\ConfidentPDR(19 Aug)\ManuHelp.ConfidentPDR.Presentation.MVC\Appointments\FileEntryManagement.aspx.cs:63
System.Web.UI.Control.PreRenderRecursiveInternal() +112
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4296

here is the Source Code :-

C#
protected override void OnPreRender(EventArgs e)
       {
           base.OnPreRender(e);
           if (Session["PdfFile"] == null)
           {
               PDFfileName.Visible = false;
               Pdf_Fram.Visible = false;
               placeHolderPdfList.Visible = true;

               System.Web.UI.WebControls.CheckBox selectAllFile = null;
               foreach (DataListItem item in pdfDataList.Controls)
               {
                   if (item.ItemType == ListItemType.Header)
                   {
                       selectAllFile = (System.Web.UI.WebControls.CheckBox)item.FindControl("selectAllFile");
                   }
               }
               if (!selectAllFile.Checked)
               {
                   BindPDFList();
               }
           }
       }


Page Load:
C#
protected void Page_Load(object sender, EventArgs e)
       {
           try
           {
               if (Session["PdfFilePath"] != null)
               {
                   Session["PdfFilePath"] = null;
               }

               if (Session["PdfFile"] == null)
               {
                   Pdf_Fram.Visible = false;
                   placeHolderPdfList.Visible = true;
               }

               if (!Page.IsPostBack)
               {
                   BindPDFList();

               }
           }
           catch
           {
               Session["PdfFilePath"] = null;
           }
       }
Posted

1 solution

According to your information, selectAllFile is null at the moment of attempt to dereference it by calling the property selectAllFile.Checked, which throws this exception.

Not to worry. You already done most of the work by pointing out the like of code where the exception was thrown. You can do it again when needed.

This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requi8res this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[^].

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

Good luck,
—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900