473,394 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Intermittent NullReferenceException Error

The following code is running in an asp.net application:

bool bLogin;
try
{
bLogin = Convert.ToBoolean(Session["UserLogin"].ToString());
}
catch
{
bLogin = false;
}

ArrayList _roles;
try
{
_roles = (ArrayList)Session["Role"];
}
catch
{
_roles = new ArrayList();
_roles.Add(0);
}

if(bLogin && _roles.Contains(3))
{
plUnsecure.Visible = false;
plSecure.Visible = true;
}
else
{
plUnsecure.Visible = true;
plSecure.Visible = false;
}

if(!_roles.Contains(3))
{
plLogin.Visible = true;
}
else
{
plLogin.Visible = false;
}

I'm getting intermittent NullReferenceException errors on the line:
if(!_roles.Contains(3))

This is confusing me because the line:
if(bLogin && _roles.Contains(3))

was executed prior to the error line and it did not generate a
NullReferenceException error.

The entire code block runs in the Page_Load method of the asp.net page. I
am unable to reproduce the error in the production or development
environments, but all production errors are logged, so I know it is occurring.

I plan on refactoring the code, but I hope someone could shed some light on
why it is not working as is.

Thanks,
Eric
Oct 4 '06 #1
2 1501
This would make sense if bLogin is false, so _roles.Contains(3) is never
tested, so I'm guessing that the session has dropped, and so Session["Role"]
returns null.

I would make this:
_roles = (ArrayList)Session["Role"];
if(_roles==null) {
_roles = new ArrayList();
_roles.Add(0);
}

I would also investigate a flags enum for the roles.

Marc
Oct 4 '06 #2
Thanks Marc. You are absolutely right.

"Marc Gravell" wrote:
This would make sense if bLogin is false, so _roles.Contains(3) is never
tested, so I'm guessing that the session has dropped, and so Session["Role"]
returns null.

I would make this:
_roles = (ArrayList)Session["Role"];
if(_roles==null) {
_roles = new ArrayList();
_roles.Add(0);
}

I would also investigate a flags enum for the roles.

Marc
Oct 4 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Fabio Papa | last post by:
Hi, I am fairly new to programming and and even newer to dotnet. I appoligise in advance if this is a dumb questions, and I would appreciate if you could answer it anyways. :) I am writing a...
2
by: Raed Sawalha | last post by:
i have a windows form(Main) with listview, when click an item in listview i open other window form (Sub) which generate the selected item from parent window in as treeview items when click any item...
0
by: deevoy | last post by:
Hi- I'm developing a asp.net web application and everything has proven fine on the dev and acceptance environment. We've moved the code up to our windows server 2003 prod environment and get the...
8
by: trinitypete | last post by:
Hi all I have a strange problem with IIS windows pass through authentication. Heres the setup IIS running with Windows Authentication for our intranet site. ACL has been set to everyone for all...
3
by: Lee Chapman | last post by:
Hi, I have a problem where my ASP.NET application occasionally generates a MissingFieldException exception. This unexpectedly happened on my development box, and so I was able to extract some...
1
by: deevoy | last post by:
Hi- I'm developing a asp.net web application and everything has proven fine on the dev and acceptance environment. We've moved the code up to our windows server 2003 prod environment and get...
6
by: William Mild | last post by:
I must be getting brain fried. I can't see the error. Create a new web form with the following code begind: Public Class test Inherits System.Web.UI.Page Public Class ReportCardData ...
0
by: Shane C | last post by:
Intermittent Error: Server was unable to process request  Object reference not set to an instance of an object I have windows application that references a XML web service on our web server. ...
8
by: Dave | last post by:
I am getting an intermittent database error on my asp page. I am using Access 2003 with classic ASP. The error is this: Microsoft JET Database Engine Error 80040e10 No value given for one or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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...
0
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
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...

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.