473,386 Members | 1,775 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,386 software developers and data experts.

System.NullReferenceException

I have a strange error that occurs on loading an XML document:
The code is:

// create a document
XmlDocument myDoc = new XmlDocument();
// and load it from file
myDoc.Load(filename);

Inside of .Load()I am getting a System.NullReferenceException in about 1 out
of 10 runs (always the same xml file).
Jan 21 '10 #1
6 2141
tlhintoq
3,525 Expert 2GB
You've described the situation.
What is your question?

If that is a custom load() method that you wrote then someplace you are not initalizing a variable before you use it.

Otherwise I will suggest that you code with a bit more "body armor" to make your programs more error resistant. Assume that everything fails and that the user is always TRYING to break your program. Your code should make sure that everything is valid before doing anything. Is the file really there? Can you open it or is it in use by another process? etc. Just because you wrote the file doesn't mean someone has come along and fraked with it afterwords, so don't assume that every value you wrote in it is still there. Always make sure you make use of multiple retry attempts and bail out of routines after a reasonable amount of time so they don't just 'hang'
Jan 21 '10 #2
My question is how do we avoid the System.NullReferenceException error. The Null reference error doesn't show up all the time in the production environment. If it happens, it happens continously 3-4 times and then starts working.

Application is built on ASP.NET, Framework 1.1 and sharepoint 2003. Nullreference exception pops up generaly when users work on the sharepoint webpart usercontrols ( Grid) to add or edit records.

We have written a custom class WebPartConfiguration to read the the custom configuration file properties. There is a method call GetValue of this class where the nullreference exception is thrown . It fails in the line configXml.Load.

GetValue(string property, Page page)
{
configXml = new XmlDocument();
configXml.Load(page.Server.MapPath(UIUtility.GetRe sourcePath("Configuration.xml")));
return configXml.SelectSingleNode("/configuration/" + property).InnerText;
}
Jan 22 '10 #3
Plater
7,872 Expert 4TB
I would guess it happens with malformed XML files.
However, since it's on a webpage, I would check to make sure a 2nd call isn't messing up the first call.
Jan 22 '10 #4
I dont see any problem with configuration xml file . its failing 1 in 10 occurences. i dont understand that part?
Jan 22 '10 #5
Plater
7,872 Expert 4TB
If the same file works sometimes and not others, its probably a race condition.
One of your calls hasn't completed yet when another one comes in and they get scrambled using the same resources
Jan 22 '10 #6
tlhintoq
3,525 Expert 2GB
My question is how do we avoid the System.NullReferenceException error.
By initialzing every variable at the time you create it; even if it is just with a dummy value. I initialize everything with values that I can look for like strings to "dummy" and ints to -1 and so on. For XML I assume you could init to an empty tag or something, right?
Jan 23 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Terrence | last post by:
I am doing some of the C# walkthroughs to transition from VB to C#. When I try to execute static void Main() { Aplication.Run(new Form1()) } I raise a 'System.NullReferenceException" in...
1
by: Rafael | last post by:
Hi, I hope I can find some help for this problem IDE: Visual Studio.NET 2003 Developer Editio Language: C# Problem: "An unhandled exception of type 'System.NullReferenceException' occurred in...
7
by: mike p. | last post by:
I have a docbook xml file, and am using standard docbook 1.61.3 xsl stylesheets to do xhtml transform. Transform works fine when using MSXML. When I try to do the following using asp.net 1.1: ...
0
by: muralidharan | last post by:
WebForm1.aspx Code: <%@ Register TagPrefix="ComponentArt" Namespace="ComponentArt.Web.UI" Assembly="ComponentArt.Web.UI" %> <ComponentArt:TreeView id="TreeView1" Height="520"...
0
by: Matthias Kwiedor | last post by:
Hi! I want to create a WebBrowser Control at runtime. So i ported the "at start creation" of the Control to a new routine. Everything works fine, but i implement the BeforeNavigateFix and 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...
1
by: msnews.microsoft.com | last post by:
I'm trying to fill an array of objects but when I add the first object I get a NullReferenceException. ----------------------------------------------------------------------------...
2
by: sxiao | last post by:
Hi, there I got a NullReferenceException when there are more than one users trying to open the same page at the same time. The senerio is: Two users logged into the web application using the...
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 ...
3
by: Alex J. | last post by:
I just started to learn C# (my background is in C++), and right now I study the sockets, TCP/IP etc... so, I found a usefull source code at:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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,...

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.