471,591 Members | 1,849 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,591 software developers and data experts.

"Object reference not set to an instance of an object" error


I'm using ASP.NET, getting an "Object reference not set to an instance of an
object" error.

In my login.aspx page I have:

string[] arrUserRoles = new string[] {"UserRole"};
Context.Items.Add("UserRoles", arrUserRoles);
Context.User = new
System.Security.Principal.GenericPrincipal(Context .User.Identity,

In another page I have

string[] arrUserRoles = {}; <-- call this line a
arrUserRoles = (string[]) HttpContext.Current.Items["UserRoles"]; <--- Call
this line b . . . error on this line
Label1.Text = arrUserRoles.Length.ToString();

If I comment out line b it works (displays 0 as it should). If I put line b
back in, it throws the error.

Any suggestions on what's wrong?

Also, how do I test for the context variable arrUserRoles being defined?


Lauchlan M

Nov 17 '05 #1
1 2138

If there is a specific recommended practice I haven't hear of it yet.

I've been using, as you put it, a "Roll your own" implementation.

The context object is a great way to go in my opinion. There are a lot of
potential pitfalls to using session variables that I just don't like so I
try to stay away from them.


S. Justin Gengo, MCP
Web Developer

Free code library at:

"Out of chaos comes order."
"Lauchlan M" <LM********@Hotmail.com> wrote in message

What is the recommended way of keeping track of this security data?

Is there some provided object that remains static over the session? Or
should I create a session variable for it (which seems to take away from the point of using MS's security framework in the first place and begins to look more like a roll-your-own exercise)


Lauchlan M
The Context object is not like a session variable or application variable. It is a stateless object meaning that it's recreated every request to the server.

However, the context object can be passed from one page to another using
Server.Transfer. Then on the second pages page load (first page load only
be sure to use If Not IsPostBack). This way you can transfer a user from

page to another and pass values from the first page to the second

through the context object.

I have a some sample code on my website, www.aboutfortunate.com, in the

library. Just search the code library for: "server.transfer".

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by chuckdfoster | last post: by
7 posts views Thread by dhnriverside | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.