none
new System.Xml.XmlDocument() cause Object reference not set to an instance of an object. RRS feed

  • Question

  • I have a class library, one method is return database connection string from a xml configuration file.
    it works fine when I ref it in my .net web service or website.
    But today, When I was writing a Windows service, It said:
    "Object reference not set to an instance of an object. "
    It happens right after this line:
     System.Xml.XmlDocument oXmlconfig =  new System.Xml.XmlDocument();

    any idea why?

    Thank you
    Wednesday, September 2, 2009 6:37 PM

Answers

All replies

  • System.Xml.XmlDocument oXmlconfig =  new System.Xml.XmlDocument();

    What's right after that line?
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    Wednesday, September 2, 2009 6:39 PM
    Moderator
  • xml.Load(ConfigFile)
    but the program doesn't even run this line
    Wednesday, September 2, 2009 6:58 PM
  • You've instantiated "oXmlConfig".  It's either "xml" or "ConfigFile" that's null.  Whatever those are, they need to be instantiated and set to values before you use them.

    Could you post more of your code?  Preferably the whole class?
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    Wednesday, September 2, 2009 7:06 PM
    Moderator
  •  public static string GetConnectionString(string ConfigFile)
            {
                string constr;
                try
                {
                    System.Xml.XmlDocument oXmlconfig =  new System.Xml.XmlDocument();//program stopped right here
                    oXmlconfig.Load(ConfigFile);
                    string server = oXmlconfig .DocumentElement.SelectSingleNode("//Database/Server").InnerText;
                    string db = oXmlconfig .DocumentElement.SelectSingleNode("//Database/Db").InnerText;
                    string user = oXmlconfig .DocumentElement.SelectSingleNode("//Database/User").InnerText;
                    string password = oXmlconfig .DocumentElement.SelectSingleNode("//Database/Password").InnerText;
                    oXmlconfig = null;
                    constr = "Data Source=" + server + ";Initial Catalog=" + db + ";User ID=" + user + ";Password=" + password;
                }
                catch (Exception ex)
                {
                   
                    throw ex;
                }
    
             
                return constr;
            }
    
    This is the entire method
    • Edited by Acherons Wednesday, September 2, 2009 7:20 PM
    Wednesday, September 2, 2009 7:16 PM
  • Do me a favor.  Recompile.  Ctrl-Shift-B.  Run it again.  Post the line that it breaks on.  I'm thinking you have a mismatch between your running version and the debug .pdb you're debugging against.  Rebuilding will fix this.  Once you have it built, go to the line that calls load.  Press F9.  A breakpoint will appear.  Run the application in the debugger.  When it hits the breakpoint, hover your mouse over ConfigFile.  What shows up?
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    Wednesday, September 2, 2009 7:19 PM
    Moderator
  • Hi David,

    Thank you for the help, but the problem is still there. here is what i did:
    1. I stopped windows service first
    2. went to release folder, delete all files
    3.. rebuild, and yes files were created under release folder
    4. start service, attach process
    5. ConfigFile is "d:\My Documents\My Projects\VS\xxxx\xxxx\bin\Release\config.xml"

    when i add variable " (new System.Xml.XmlDocument()) " in to watch, it return:
    "Cannot evaluate expression because a thread is stopped at a point where garbage collection is impossible, possibly because the code is optimized.    "

    But i have turned off optimized in build configuration
    Wednesday, September 2, 2009 7:43 PM
  • Build to debug, not release, and try the whole thing again.  Install your service under debug, attach to the process, etc, etc, etc.  The code is optimized because you've built to Release mode.  I'm 99.999% sure your problem isn't with the XmlDocument constructor.  Constructors don't return null.  Something else is throwing here.
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    Wednesday, September 2, 2009 7:46 PM
    Moderator
  • Hi, David

    I solved this problem finally, Im so stupid put get connection string part in a thread(there are multiple thread), i should init them in constructor

    anyway, good to know thats the different between "debug" and "release"
    Wednesday, September 2, 2009 8:20 PM
  • It would also be immensly useful to look at the stack trace and the Threads window in debug mode.
    Mahesh
    Wednesday, September 2, 2009 8:44 PM