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

  • Thread starter Thread starter Lauchlan M
  • Start date Start date
L

Lauchlan M

Hi.

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,
arrUserRoles);

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?

Thanks!

Lauchlan M
 
Lauchlan,

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.

Sincerely,

--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
 
Back
Top