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

L

Lauchlan M

Hi.

Usin 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
 
J

Jeff Gaines

Hi.

Usin 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

This is a poorly (non?) documented error message which I see
constantly.

It seems to me it is often thrown up if there is a different problem,
i.e. if there is something wrong with
'HttpContext.Current.Items["UserRoles"]' it may throw this error.

It appears constantly when accessing the Win API if the way it is
called or some of the parameters are wrong.

Can you break it down any and try
'HttpContext.Current.Items["UserRoles"]' on its own somehow?
 
J

Jon Skeet

Jeff Gaines said:
This is a poorly (non?) documented error message which I see
constantly.

It seems to me it is often thrown up if there is a different problem,
i.e. if there is something wrong with
'HttpContext.Current.Items["UserRoles"]' it may throw this error.

It appears constantly when accessing the Win API if the way it is
called or some of the parameters are wrong.

Can you break it down any and try
'HttpContext.Current.Items["UserRoles"]' on its own somehow?

I don't think it's that poorly documented or mysterious, actually. It's
almost always because you've got a reference which you're treating as
non-null when it's actually null.

My guess is that HttpContext.Current.Items["UserRoles"] is returning
null - the real question is why.
 
J

Jeff Gaines

Jeff Gaines said:
This is a poorly (non?) documented error message which I see
constantly.
[snipped]

I don't think it's that poorly documented or mysterious, actually. It's

[snipped]

Well a search for this error message in help returns zilch.

Perhaps I'm looking in the wrong place?
 
J

Jon Skeet

Jeff Gaines said:
Jeff Gaines said:
This is a poorly (non?) documented error message which I see
constantly.
[snipped]

I don't think it's that poorly documented or mysterious, actually. It's

[snipped]

Well a search for this error message in help returns zilch.

Perhaps I'm looking in the wrong place?

You're not looking at the actual exception being thrown - the message
just happens to be the message given by the exception. Look up
NullReferenceException instead and you'll find a bit of information.

The first thing I look at when an exception is thrown is the type of
exception, not the message.
 
W

Wim Hollebrandse

In addition to what Jon's mentioned - it might be worth debugging your code
and see on what line you are actually getting this error. Then, all you do
is print the values of the objects used on that line in the Command Window,
and you know you are trying to use a null object as if it was a properly
referenced object.

The error message is clear as crystal.

Cheers,
Wim Hollebrandse
--
Wimdows Components
..NET components - professional, robust and affordable.
http://www.wimdows.com
--

Jon Skeet said:
Jeff Gaines said:
This is a poorly (non?) documented error message which I see
constantly.
[snipped]

I don't think it's that poorly documented or mysterious, actually. It's

[snipped]

Well a search for this error message in help returns zilch.

Perhaps I'm looking in the wrong place?

You're not looking at the actual exception being thrown - the message
just happens to be the message given by the exception. Look up
NullReferenceException instead and you'll find a bit of information.

The first thing I look at when an exception is thrown is the type of
exception, not the message.
 
J

Jeff Gaines

In addition to what Jon's mentioned - it might be worth debugging your code
and see on what line you are actually getting this error. Then, all you do
is print the values of the objects used on that line in the Command Window,
and you know you are trying to use a null object as if it was a properly
referenced object.

The error message is clear as crystal.

Cheers,
Wim Hollebrandse

I must be running a different program!

I am more used to Borland compilers, when they pop up with an error
message I press 'F1' and it takes me to an explanation of the error -
which I find useful.

In VS7 pressing 'F1' tells me there has been an error, well I know
that, what I want is information on avoiding it.

Searching for 'NullReferenceException' as suggested by Jon leads to a
not very exciting explanation of how to construct a
NullReferenceException object, I don't want to thanks, I just got one
automagically!

The meaning of the error looks clear on the face of it but when
calling the Win API, as I said earlier, it is this error that gets
thrown up if a parameter is wrong, and that is not very helpful.

Anyway I doubt if this helps the original writer who by now has
probably analysed the problem line, fixed it and is writing his next
program :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top