开发者

ASP.NET NullReferenceException for get_Session()

public class MessageHelper : System.Web.UI.MasterPage
{
    public MessageHelper()
    {

    }

    public string Message
    {
        set { Session["Message"] = value; }
        get
        {
            if (Session["Message"] != null)
            {
            开发者_如何学编程    var msg = Session["Message"] as string;
                Session["Message"] = "";
                return msg;
            }
            return "";
        }
    }

    public string ErrorMsg
    {
        set { Session["Error"] = value; }
        get
        {
            if (Session["Error"] != null)
            {
                var err = Session["Error"] as string;
                Session["Error"] = "";
                return err;
            }
            return "";
        }
    }
}


[NullReferenceException: Object reference not set to an instance of an object.]
   System.Web.UI.UserControl.get_Session() +15
   WebApplication1.MessageHelper.get_ErrorMsg() in ..file.master.cs:71

where line 71 is: if (Session["Error"] != null)

what am I doing wrong here?!

EDIT (transcribed from original author):

@David,

here is AdminMaster.master.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;

namespace WebApplication1
{
    public partial class AdminMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MessageHelper msg = new MessageHelper();

            if (msg.ErrorMsg != "")
            {
                // do something
            }
            if (msg.ErrorMsg != "")
            {
                // do something
            }
        }
    }
    public class MessageHelper : System.Web.UI.MasterPage
    {
        public MessageHelper()
        {

        }

        public string Message
        {
            set { System.Web.HttpContext.Current.Session["Message"] = value; }
            get
            {
                if (System.Web.HttpContext.Current.Session["Message"] != null)
                {
                    var msg = System.Web.HttpContext.Current.Session["Message"] as string;
                    System.Web.HttpContext.Current.Session["Message"] = "";
                    return msg;
                }
                return "";
            }
        }

        public string ErrorMsg
        {
            set { System.Web.HttpContext.Current.Session["Error"] = value; }
            get
            {
                if (System.Web.HttpContext.Current.Session["Error"] != null)
                {
                    var err = System.Web.HttpContext.Current.Session["Error"] as string;
                    System.Web.HttpContext.Current.Session["Error"] = "";
                    return err;
                }
                return "";
            }
        }
    }
}

so it does inherit from System.Web.UI.MasterPage, my bad.

i want the MessageHelper to be accessed from different pages on the site. all of my pages use the Master file, that's why i put the MessageHelper in the master file.

what is wrong here?


During debugging, can you confirm that Session is not null? Try referencing it fully-qualified as System.Web.HttpContext.Current.Session within this class and see if that helps any.

Edit: In response to the non-answer answer that you posted...

It's not recommended to put that helper class in the same file as your master page. Put it in its own file named for the class. (There's probably debate over whether every class should have its own file, but in this particular case it's clear that the two classes in this one file are very much unrelated and shouldn't be together.)

The class can have its own namespace, such as WebApplication1.Helpers (though I recommend in the future using something more descriptive than WebApplication1, but don't try to change it here because it'll cause errors elsewhere in the project), and other class files can reference that namespace with using WebApplication1.Helpers in order to use that class.

Separating classes into an intuitive structure in the project (or multiple projects, as things grow in complexity) will make it easier to support in the future.

And, seeing the whole file, the helper class definitely should not inherit from MasterPage. It doesn't need to, and doing so adds things to that class that shouldn't be there.


I'm a bit confused by what you're trying to achieve with the MessageHelper class.

If it is code common to your master pages then you should surely be inheriting AdminMaster from MessageHelper.

eg.

public partial class AdminMaster : MessageHelper

If not, I don't understand why MessageHelper needs to inherit from MasterPage?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜