1
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?

4
  • Session itself could be null. Commented Dec 3, 2010 at 17:50
  • @Marcie, indeed. @NATTO, where is the code that is invoking/referencing .ErrorMsg? Commented Dec 3, 2010 at 17:50
  • how come? so how do i solve this error? Commented Dec 3, 2010 at 17:51
  • its protected void Page_Load(object sender, EventArgs e) { ..... Commented Dec 3, 2010 at 17:53

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Okay, that seemed to solve the problem.. what could cause this? should I inherit from... where?!?
@NATTO: Not sure about all the details off the top of my head, but HttpContext.Current is something that pages have pre-referenced for you. You're creating a separate class, and though it inherits from MasterPage (why, by the way? it's strange to extend a class as something that doesn't do what it's meant to do), it doesn't seem to be aware of the HttpContext.Current singleton and is instead just referencing an empty Session.
well, I put it in a separate class because I want it to be accessed from every place, meaning the class sits on the master file, and could be accesses from different pages, am I wrong?
@NATTO: A separate class is good, and for the right reason. What I don't see from this code is why that class inherits from MasterPage. Is that MessageHelper class actually being used as a master page? If not, it shouldn't inherit from it.
it doesn't inherit from it.. its on the master.cs file, under the same namespace, and its defined: public class MessageHelper : System.Web.UI.MasterPage, so it inherit from System.Web.UI.MasterPage..
|
0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.