Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8355401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:51:04+00:00 2026-06-09T09:51:04+00:00

Using ASP.NET MVC3. What I’m trying to do is initialize a Controller field/property with

  • 0

Using ASP.NET MVC3.

What I’m trying to do is initialize a Controller field/property with an Application state variable.

I.e. in my Application_Start() method I have

Application["stats"] = new Stats(); //this is fine

Accessing this via a Controller method, e.g.

public ActionResult Index()
{
  return View(HttpContext.Application["stats"]); //this is also fine
}

works fine too.

//blows up with a 'Object reference not set to an object' error.
private Stats stats;
public HomeController()
{
    stats= (Stats)(HttpContext.Application["stats"]);
}

Anyone able to explain what I’m failing to understand + how to go about fixing the problem ? If there’s a better way, let me know (and just in case you’re wondering, I’m not trying to load up application configuration or anything; just to keep live stats on the state of the website)

Thanks in advance

-Marcin

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:51:06+00:00Added an answer on June 9, 2026 at 9:51 am

    The HttpContext is not yet initialize inside a controller constructor and is null.

    If you need to access any HttpContext related stuff this could be done at most early inside the Initialize method:

    public class HomeController: Controller
    {
        private Stats stats;
    
        protected override void Initialize(RequestContext requestContext)
        {
            base.Initialize(requestContext);
            stats = (Stats)(HttpContext.Application["stats"]);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: ASP.NET MVC3 how to excute action method of controller using timer with
I'm using ASP.NET MVC3 and trying to validate an URL field using DataAnnotationsExtensions. It's
I'm using ASP.NET MVC3. I've used Output Caching on controller method. [OutputCache(Duration = 3660,
I am going to build my application using asp.net mvc3 and nhibernate 3.2. I
I am creating an ASP.NET MVC3 application using NHIBERNATE. I have a base class
I am using ASP.NET MVC3 for my web application. To display a button in
I am trying to learn MVC using ASP.NET MVC3. Most of the tutorials I
I am using Asp.net MVC3 and trying to do a simple Ajax post to
I've got a simple web application using ASP.NET MVC3 and Ninject.Web.MVC (the MVC3 version).
I am currently refactoring my code for a web application developed using ASP.NET MVC3

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.