某些方法可用类,但其他方法不可用

时间:2013-11-29 21:34:08

标签: c# asp.net nullreferenceexception

这是背后的代码:

public partial class ViewDetailedActivity : System.Web.UI.Page
{
    public AstraFunctions vdaa = new AstraFunctions();
    public AstraFunctions vdaabdt = new AstraFunctions();
    public AstraFunctions vdaabdc = new AstraFunctions();
    protected void Page_Load(object sender, EventArgs e)
    {
            if (!IsPostBack)
            {
                AstraHdr.Set_Title("View Detailed ASTRA Activity");
                SetVDAAPanelView(); // <----  THIS WORKS!
            }
    }
/*.............*/
    protected void SetVDAAPanelView()
    {
        try
        {
//THIS ALL WORKS
            string sSqlCommand = "Astra_ReportActivity";
            SqlParameter[] SQLParameters = null;
            vdaa.SetUp(gvViewDetailedASTRAActivity, sSqlCommand, SQLParameters);
            vdaa.SetDataSet();
            Response.Write(vdaa.gv.ID.ToString();
            pnlViewDetailedASTRAActivity.Visible = true;
            pnlViewDetailedASTRAActivityByDate.Visible = false;
            pnlViewDetailedASTRAActivityByDODACC.Visible = false;
        }
        catch (Exception ex)
        {
            Response.Write("Error in SetVDAAPanelView():  " + ex);
        }
    }
/*..............*/
    protected void gvViewDetailedASTRAActivity_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            vdaa.gv.ID.ToString(); // DOES NOT work!
            //vdaa.gv_PageIndexChanging(sender, e); // this DOES NOT work!
        }
        catch (Exception ex)
        {
            Response.Write("Error in gvViewUnprocessedReceipts_PageIndexChanging():  " + ex);
        }
    }
}

当我触发gvViewDetailedASTRAActivity_PageIndexChanging时,我收到以下错误:

  

gvViewDetailedASTRAActivity_PageIndexChanging()中的错误:System.NullReferenceException:未将对象引用设置为对象的实例。在[filename]处的VIM.ASTRA.ViewDetailedASTRAActivity.gvViewDetailedASTRAActivity_PageIndexChanging(Object sender,GridViewPageEventArgs e):line [linenumber]

我可以访问SetVDAAPanelView()但不是gvViewDetailedASTRAActivity_PageIndexChanging(object sender, GridViewPageEventArgs e)中的类对象 - 是什么给出了?

1 个答案:

答案 0 :(得分:0)

尝试将对象存储在会话中

public AstraFunctions vdaa;
protected void Page_Load(object sender, EventArgs e)
{
        if (!IsPostBack)
        {
          vdaa =  Session["vdaa"] != null ? 
                  (AstraFunctions)Session["vdaa"] : new AstraFunctions();
          ...//do somthing with vdaa 
          Session["vdaa"] = vdaa;
        }
}


protected void gvViewDetailedASTRAActivity_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
           AstraFunctions vdaa = (AstraFunctions)Session["vdaa"]  
            vdaa.gv.ID.ToString();
            vdaa.gv_PageIndexChanging(sender, e); 
        }
        catch (Exception ex)
        {
            Response.Write("Error in gvViewUnprocessedReceipts_PageIndexChanging():  " + ex);
        }