473,883 Members | 1,623 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.A dd("UserRoles" , arrUserRoles);
Context.User = new
System.Security .Principal.Gene ricPrincipal(Co ntext.User.Iden tity,
arrUserRoles);

In another page I have

string[] arrUserRoles = {}; <-- call this line a
arrUserRoles = (string[]) HttpContext.Cur rent.Items["UserRoles"]; <--- Call
this line b. error on this line
Label1.Text = arrUserRoles.Le ngth.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 19945
On Sat, 16 Aug 2003 18:54:59 +1000, "Lauchlan M"
<LM********@Hot mail.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.Securit y.Principal.Gen ericPrincipal(C ontext.User.Ide ntity,
arrUserRoles );

In another page I have

string[] arrUserRoles = {}; <-- call this line a
arrUserRoles = (string[]) HttpContext.Cur rent.Items["UserRoles"]; <--- Call
this line b. error on this line
Label1.Text = arrUserRoles.Le ngth.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.Cu rrent.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.Cu rrent.Items["UserRoles"]' on its own somehow?

Nov 15 '05 #2
Jeff Gaines <je**@jgaines.c o.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.Cu rrent.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.Cu rrent.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.Cur rent.Items["UserRoles"] is returning
null - the real question is why.

--
Jon Skeet - <sk***@pobox.co m>
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.co m> wrote:
Jeff Gaines <je**@jgaines.c o.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.c o.uk> wrote:
Jeff Gaines <je**@jgaines.c o.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
NullReferenceEx ception 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.co m>
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.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Jeff Gaines <je**@jgaines.c o.uk> wrote:
Jeff Gaines <je**@jgaines.c o.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
NullReferenceEx ception 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.co m>
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.c om> 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 'NullReferenceE xception' as suggested by Jon leads to a
not very exciting explanation of how to construct a
NullReferenceEx ception 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
1546
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" error on this particular line. All my other references seem correct. ??? Muench
7
36731
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 to duplicate -- it just show up... I've heard that spurious queries that use Now() or Date() or some other date
1
5489
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 one help me out to resolve this issue. Thanks in advance, Kamal
1
2376
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"}; Context.Items.Add("UserRoles", arrUserRoles); Context.User = new
2
1635
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 below. This is where I'm getting the "Object reference not set to an instance.." error <% response.writefile(thelink())%>. The function that line is referring to is below. The record is being retrieved from the database according to its...
2
2412
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. Both have VS.NET installed and both have MS Outlook installed. What do I need done to the machine that doesn't work? Or is something wrong with the code? Dim email as New System.Web.Mail.Message email.To = "me@me.com" email.Subject = "Test...
7
3799
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 my cs file, but when I try and type the following line... Exception objErr = Server.GetLastError().GetBaseException();
2
2981
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 to the same page at the top of my form alerting the user that their request has been approved or denied. However, the end result is an error message: Error Message: Object reference not set to an instance of an object.
5
2351
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 showClientListForUser() { string fkeys = new string; fkeys = txtUserName.Text; fkeys = txtUserCompanyNumber.Text;
0
9778
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10730
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10405
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9559
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7959
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5784
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4205
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3228
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.