System.NullReferenceException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raju101076
    New Member
    • Jan 2010
    • 3

    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(file name);

    Inside of .Load()I am getting a System.NullRefe renceException in about 1 out
    of 10 runs (always the same xml file).
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    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'

    Comment

    • raju101076
      New Member
      • Jan 2010
      • 3

      #3
      My question is how do we avoid the System.NullRefe renceException 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 WebPartConfigur ation 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.Map Path(UIUtility. GetResourcePath ("Configuration .xml")));
      return configXml.Selec tSingleNode("/configuration/" + property).Inner Text;
      }

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        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.

        Comment

        • raju101076
          New Member
          • Jan 2010
          • 3

          #5
          I dont see any problem with configuration xml file . its failing 1 in 10 occurences. i dont understand that part?

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            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

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              My question is how do we avoid the System.NullRefe renceException 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?

              Comment

              Working...