473,503 Members | 2,289 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
6 19928
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1527
by: J. Muenchbourg | last post by:
I have a label object that gets a dynamic value thrown into it on a button submit event: lblMessage.InnerText = txtName.Value but i get "Object reference not set to an instance of an object"...
7
36675
by: deko | last post by:
I'm getting intermittent "Object Invalid or No Longer Set" errors in my Access 2002 mdb. What causes these errors? Has anyone dealt with this before? I can't trace it because it's not easy...
1
5436
by: Kamal | last post by:
I am trying to send mail through smtp. smtp service is running on my machine. But every time during the smtpmail.send(msg) call gives "Could not access 'CDO.Message' object." error. Could some...
1
2340
by: Lauchlan M | last post by:
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"};...
2
1615
by: prince -=nore=- | last post by:
I have a page, where I'm retrieving a file location from a database and outputting it's contents (HTML) onto a web page. On the aspx page, I have referred to the code behind it using the line...
2
2394
by: chuckdfoster | last post by:
I am getting a "Could Not Access CDO.Message Object" Error when I try to use the following code to send an email via ASP.NET. When I run this on one machine it works, on another one it doesn't. ...
7
3780
by: dhnriverside | last post by:
Hi peeps I'm just following this HOW-TO from MSDN.. http://support.microsoft.com/default.aspx?scid=kb;en-us;306355 But I've got a problem. I've adding the #using System.Diagnostics; line to...
2
2964
by: louie.hutzel | last post by:
This JUST started happening, I don't remember changing any code: When I click the submit button on my form, stuff is supposed to happen (which it does correctly) and a result message is posted back...
5
2294
by: piyumi80 | last post by:
hi, i write the following code to get a specific data row from the data set.but it generates the "Object reference not set to an instance of an object.".....error private void...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7291
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7012
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.