我有一個函數可以獲取傳遞給它的字符串。在測試函數時,我得到了傳遞給它的字符串參數的空引用異常,即使字符串不爲空,我也不明白爲什麼我會得到這個錯誤。我有一個屏幕截圖如下對非空值的NullReference
我用虛值,同時驗證在的SelectSingleNode函數的字符串參數和NEWVALUE字符串參數傳遞給我的功能,它們都包含值,所以我不不明白爲什麼它拋出一個空引用異常。爲了清楚起見,該函數的目的是將值寫回XML文件的節點。
UPDATE
對不起,不張貼代碼
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
這裏是我與
<?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">
<Username>[email protected]</Username>
<Password>Password1</Password>
<ProductType>MyProduct</ProductType>
<DirectoryEntryPath>LDAP://myDomain</DirectoryEntryPath>
<SMTPDefaultOnly>True</SMTPDefaultOnly>
<Logo>myLogo.gif</Logo>
</Config>
是的,SlectSingleNode函數沒有返回值工作的XML文件。我剛開始使用XPath,這在我上週使用時似乎很有用。我不知道爲什麼它現在停止工作。
UPDATE2:
明白了,愚蠢的錯誤。我曾在nodeToMod Set方法
Set(ByVal value As String)
setValue("UserName", value.ToString)
_userName = value
End Set
您可能希望向我們展示了更多的代碼。如果它給出了空的異常,那麼在賦值爲innertext時,「newValue」肯定是空的。 – loxxy 2010-09-27 17:10:57
@loxxy,'newValue'沒有在該行被取消引用。它不能成爲NRE的原因。 – 2010-09-27 17:12:54
@kirk,這個問題已經被更多的信息編輯。解除引用並不明顯。 – loxxy 2010-09-28 11:10:55