0

I have report on page that if I walk away for 20 minutes I get the error message Object reference not set to an instance of an object

AMFM.ReportViewer.GenerateReport(DataTable dt) in c:\Users\gorella\Documents\Visual Studio 2013\Projects\Web_Applications\AMFM\AMFM\ReportViewer.aspx.cs:76

AMFM.ReportViewer.Page_Init(Object sender, EventArgs e) in c:\Users\gorella\Documents\Visual Studio 2013\Projects\Web_Applications\AMFM\AMFM\ReportViewer.aspx.cs:65

First problem: The path above is not even the user profile or path that I am running this run from.

Second problem is how to get rid of this error when the user tried to refresh the page.

This code was copied from a TFS. This is the code on line 65 and 76

private void Page_Init(object sender, EventArgs e) {
    DataTable dt = (DataTable)Session["dataset"];

    String query = (String)Session["query"];
    //System.Diagnostics.Debug.WriteLine("********************" + query);
    //System.Diagnostics.Debug.WriteLine("########################" + dt.Rows.Count.ToString());
    if (dt == null) {
        System.Diagnostics.Debug.WriteLine("THIS IS A TEST EVENT MESSAGE Response Header----------- THIS IS A TEST EVENT MESSAGETHIS IS A TEST EVENT MESSAGETHIS IS A TEST EVENT MESSAGETHIS IS A TEST EVENT"); ;
    }
    GenerateReport(dt);
}

protected void GenerateReport(DataTable dt) {
    // Get the dataset from the session state. Passed in from Reports.aspx.
    // This way the dataset is only generated once.
    //Report treport = new Report();
    // Get the report node from the session state. Passed in from Reports.aspx

    ReportTreeNode rn = (ReportTreeNode)Session["report"];

    System.Diagnostics.Debug.WriteLine(String.Format("### {0}", rn.Text));
    //Report r = new Report();
    System.Diagnostics.Debug.WriteLine(String.Format(" {0}",
        rn.treport.ToString())
    );

    rn.treport.ReportParameters["title"].Value = rn.Text + " Report";
    rn.treport.ReportParameters["title2"].Value = rn.title2;
    rn.treport.DataSource = dt;
    TReportViewer1.ReportSource = rn.treport;    
    //TReportViewer1.Report = rn.treport;
    // do a refresh if needed.
    //TReportViewer1.RefreshReport();
}

Need help troubleshooting. I at least want to make it too where if this exception comes up to redirect to the homepage

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794

2 Answers2

0

Im not entirely sure on this but this is the only thing i can imagine would be related to the amount of time you step away from the page, the session is probably expiring on the server.

Where you have the Page_Init you need to modify it so that you check if that query is null

private void Page_Init(object sender, EventArgs e)
    {
        DataTable dt = (DataTable)Session["dataset"];

        String query = ""
        if(Session["query"]!= null){
            query = (String)Session["query"];
        }
        if (dt == null)
        {
            System.Diagnostics.Debug.WriteLine("THIS IS A TEST EVENT MESSAGE Response Header----------- THIS IS A TEST EVENT MESSAGETHIS IS A TEST EVENT MESSAGETHIS IS A TEST EVENT MESSAGETHIS IS A TEST EVENT"); ;
        }
        GenerateReport(dt);
    }

Then you need to do a check on the query when you next go to use it to make sure its not an empty string.

Failing this, attach visual studios debugger to it and let it show you where the null reference is coming from.

I wouldnt suggest redirecting errors to a non-informative homepage, while this may appear more presentable its actually just a dirty work-around to a problem that needs solving, and could make future problems harder to fix creating a snowball effect.

James T
  • 1,155
  • 4
  • 17
  • 39
  • It gives the An exception of type 'System.NullReferenceException' occurred in AMFM.dll but was not handled in user code on the first line of the generate report method. – Lawrence Ibekwe Oct 26 '15 at 17:03
  • Are you using an external library or API? – James T Oct 27 '15 at 08:39
  • Yes an external library – Lawrence Ibekwe Oct 27 '15 at 14:26
  • Why is it if I add if (rn == null){ Response.Redirect("Default.aspx"); Server.Transfer("Default.aspx"); } Just below the ReportTreeNode rn = (ReportTreeNode)Session["report"]; It will still redirect me to the NullReference error page? – Lawrence Ibekwe Nov 02 '15 at 22:18
  • because it doesnt want to be **below** the `ReportTreeNode rn = (ReportTreeNode)Session["report"];` because when it his that section of code and `Session["report"] == null` its going to try and cast `null` to type of `ReportTreeNode` ending in a null reference exception. you need to do your nullcheck **before** using something thats null – James T Nov 03 '15 at 08:47
0

The error may be occurring due to session timeout which is by default 20 minutes and your session variable lose data.

private void Page_Init(object sender, EventArgs e) {
    DataTable dt;

    String query = "";
    if (Session["query"] != null) {
        query = (String)Session["query"];
    }
    if (Session["dataset"] != null) {
        dt = (DataTable)Session["dataset"];
        GenerateReport(dt);
    }
    else {
        System.Diagnostics.Debug.WriteLine("THIS IS A TEST EVENT MESSAGE Response Header----------- THIS IS A TEST EVENT MESSAGETHIS IS A TEST EVENT MESSAGETHIS IS A TEST EVENT MESSAGETHIS IS A TEST EVENT"); ;
    }
}

Also in the GenerateReport method check for any other session variables like ReportTreeNode etc.

protected void GenerateReport(DataTable dt) {
    ReportTreeNode rn;
    if (Session["report"] != null) {
        rn = (ReportTreeNode)Session["report"];
        rn.treport.ReportParameters["title"].Value = rn.Text + " Report";
        rn.treport.ReportParameters["title2"].Value = rn.title2;
        rn.treport.DataSource = dt;
        TReportViewer1.ReportSource = rn.treport;    
    }
}
haraman
  • 2,744
  • 2
  • 27
  • 50
  • If the session variable is null . What should i put in the else statement to resolve this exception/create a new session/or redirect to the homepage? – Lawrence Ibekwe Oct 26 '15 at 18:09
  • If `Session["query"]` is null, it simply means you'll have to assign a value to variable `query`manually, if Session["dataset"] is null it means there is no data in `dataset`, reload your dataset and so on and that can be done in else block. Or to redirect to homepage in else block simply use `Response.Redirect` or equivalent as per your requirement and approach – haraman Oct 26 '15 at 18:17
  • When i put Server.Transfer("Default.aspx"); is still redirect to the error page – Lawrence Ibekwe Oct 26 '15 at 18:52
  • Check `Page_Load`, `Page_Init` or other events occurring on page load of `Default.aspx` you may be accessing any session variables there too. You must understand that once the session expires all the variables stored in it may lose their values. Always apply checks that whether session variable has a value or not, if not then you must assign some default value before using them. An alternate could be to set a much longer session timeout. – haraman Oct 26 '15 at 19:08
  • But why is the path wrong? – Lawrence Ibekwe Oct 27 '15 at 14:25
  • Which path? If you are talking about `Default.aspx` in `Server.Transfer()` then you may try using `~/Default.aspx` if calling from any sub-folder. – haraman Oct 27 '15 at 15:06
  • No im running the program that i download from tfs on a different prole path than the path in the error message – Lawrence Ibekwe Oct 27 '15 at 20:54
  • tfs is not my forte, so cann't say anything about that. However, regarding paths, i would just suggest to use debug mode and just check if it is really `Default.aspx` or `~/Default.aspx` that is giving errors or any other statement. You can also try using relative path for folder such as `../Default.aspx` to access file in parent folder. – haraman Oct 28 '15 at 14:27
  • Why is it if I add if (rn == null){ Response.Redirect("Default.aspx"); Server.Transfer("Default.aspx"); } Just below the ReportTreeNode rn = (ReportTreeNode)Session["report"]; It will still redirect me to the NullReference error page? – Lawrence Ibekwe Nov 02 '15 at 22:18
  • Have you debugged that `Session["report"]` is not null before `ReportTreeNode rn = (ReportTreeNode)Session["report"];`. Make it a habit to always check session variables for nulls before using them as in the second part of the answer above. Try to set a breakpoint in the code and do a statement by statement step through code to ascertain the root of the exception. – haraman Nov 04 '15 at 15:29