views:

63

answers:

3

I have a function that gets a string passed to it. When testing the function, I'm getting a null reference exception on the string parameter that gets passed to it, even though the string is not null, I don't understand why I'm getting this error. I have a screen shot below

alt text

I used a dummy value to verify both the string parameter in the SelectSingleNode function and the newValue string parameter being passed to my function and they both contain values, so I don't understand why it's throwing a null reference exception. Just for clarity, the purpose of the function is to write values back to the nodes of an XML file.

UPDATE

Sorry for not posting code

Private Sub setValue(ByVal nodeToMod As String, ByVal newValue As String)
        ''Test writing to xml config file
        Dim dummy As String = "Config/" & nodeToMod
        Dim xmlDoc As New XmlDocument
        Using fs As FileStream = New FileStream(HttpContext.Current.Server.MapPath("~/XML_Config/Config.xml"), FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)
            xmlDoc.Load(fs)
            Dim foo As XmlNode = xmlDoc.SelectSingleNode(dummy)
            Console.WriteLine(foo.InnerXml)
            fs.Seek(0, SeekOrigin.Begin)
            fs.SetLength(0)
            xmlDoc.Save(fs)
        End Using
    End Sub

And here is the XML file I'm working with

<?xml version="1.0" encoding="utf-8"?>
<Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  <Username>[email protected]</Username>
  <Password>Password1</Password>
  <ProductType>MyProduct</ProductType>
  <DirectoryEntryPath>LDAP://myDomain</DirectoryEntryPath>
  <SMTPDefaultOnly>True</SMTPDefaultOnly>
  <Logo>myLogo.gif</Logo>
</Config>

Yes, the SlectSingleNode function is not returning a value. I just started working with XPath, this seemed to have worked when I was using last week. I'm not sure why it has stopped working now.

UPDATE2:

Got it, stupid mistake. I had nodeToMod being passed as "UserName" instead of "Username" in the Set method

        Set(ByVal value As String)
            setValue("UserName", value.ToString)
            _userName = value
        End Set
A: 

Ensure that the FileStream object you're creating doesn't come out null. Maybe it can't find the config.xml file?

Gallen
+5  A: 

The null reference that is being complained about is the result of the call to SelectSingleNode. That is, when the xpath formed by concatenating /Config/ and the contents of nodeToMod is evaluated against the document, no node matches. So you get null, and attempting to set the InnerText of null gives the exception.

We would really need to see nodeToMod and the xml file itself to help further. Also, please post code rather than screenshots!

AakashM
A: 

It's likely that the result of SelectSingleNode is null.

Change that line to two lines, and it will be easier to see the problem in the debugger:

Dim node = xmlDoc.SelectSingleNode(dummy)
node.InnerText = newValue
Larsenal