By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Sat, 16 Aug 2003 18:54:59 +1000, "Lauchlan M"
<LM********@Hotmail.com> wrote:
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(Contex t.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?

Nov 15 '05 #2

P: n/a
Jeff Gaines <je**@jgaines.co.uk> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #3

P: n/a
On Sat, 16 Aug 2003 10:34:40 +0100, Jon Skeet <sk***@pobox.com> wrote:
Jeff Gaines <je**@jgaines.co.uk> wrote:
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?
Nov 15 '05 #4

P: n/a
Jeff Gaines <je**@jgaines.co.uk> wrote:
Jeff Gaines <je**@jgaines.co.uk> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #5

P: n/a
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" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Jeff Gaines <je**@jgaines.co.uk> wrote:
Jeff Gaines <je**@jgaines.co.uk> wrote:
> 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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #6

P: n/a
On Sun, 17 Aug 2003 01:17:08 +0100, "Wim Hollebrandse"
<wimATwimdows.com> wrote:
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 :-)

Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.