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 8279863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:28:02+00:00 2026-06-08T09:28:02+00:00

I have an object MyObject and I’m storing that in the session. I write

  • 0

I have an object MyObject and I’m storing that in the session.

I write this:

MyObject TheObject = HttpContext.Current.Session["TheObjInSession"] as MyObject;

I’m getting error Object reference not set to an instance of an object Why? I thought that if you use as to cast and the cast produces an error then it just returns null and execution continues. How should I rewrite this line?

Thanks for your suggestions.

PS: The error is produced because I removed the enable session line [WebMethod(EnableSession = true)] I’ll put the line back in but I want to make sure there are no exceptions in case anything goes wrong.

  • 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-08T09:28:04+00:00Added an answer on June 8, 2026 at 9:28 am

    Chances are the Session is triggering it (it either doesn’t exist or hasn’t been established). This is common in handlers if you haven’t marked the class to manage sessions.

    Yes, you’re right, as will attempt a cast or result in a null, but that’s only assuming you’re left-hand object is valid and has a value to attempt a cast on.

    To explain it a bit more in depth:

    HttpContext.Current.Session["Foo"]
    HttpContext                         Probably fine
    HttpContext.Current                 May not exist
    HttpContext.Current.Session         Most likely cause (no access to session)
    

    Which essentially makes referencing a key off the Session element impossible if Session itself doesn’t exist.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have an object MyObject that looks like this: public class MyObject
In C#, suppose you have an object (say, myObject ) that is an instance
I'm really scratching my head on this one: I have an object (MyObject) which
I have a serialized object MyObject that contains integer foo. I set a value
I have an instance of an object myObject that has a few UIImage objects
I have this object: const myObject = { "a":"a", "b":{ "c":"c", "d":{ "e":"e", "f":{
I have an object model called MyObject that I'm serializing to a json string
I have an object like this: public class MyObject{ public int Prop1 {get;set;} }
I have this object : public class MyObject { public int Id { get;
I have an object, in an array. Doing this: alert(myObject.cats[1].nickname); will output 'fluffykins' or

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.