2013-12-11 122 views
-2

我是在一個項目,在用戶之間共享播放列表的工作的一個實例,我使用的是XML文檔「playlist.xml」VB.net空引用異常:對象引用不設置到對象

以下是是給我的空引用異常

<WebMethod()> _ 
    Public Function getList(ByVal listNick As String) As String 

    Dim root As XmlNode = getDocument() 
    Dim number As Double 
    Dim n As XmlNodeList 
    n = root.SelectNodes("//playlist") 
    number = n.Count 

    Dim found As Boolean = False 
    'For Each node As XmlNode In n 
    Dim the_list As String = root.SelectSingleNode("playlists/playlist[@name='" + listNick + "']").InnerXml 
    Return the_list 
    'n = root.SelectSingleNode(「WebShop/Departments/Department[@name=’Hardware’]) 

    'Next 

    If (Not found) Then 
     'Return "Not found among: " + number.ToString + "\n" + "did find: " + n.Item(1).Value ---to test for errors 
     Return "0" 
    Else 
     Return "[getList] if you read this, jamie has failed his logic somewhere." 
    End If 


End Function 

和這裏的代碼是堆棧跟蹤:

System.NullReferenceException:未設置爲一個對象的實例對象引用。 at Service.getList(String listNick)in C:\ Users \ jamie \ Dropbox \ UNIVERSITY \ WebSite \ App_Code \ Service.vb:line 36 - line 11 on the code i posted

有人請儘快提供一些幫助,這是爲期2天到期的單元課程:/

回答

0

我的猜測是rootn爲空。

通過把一個破發點上以下行驗證此:

Dim number As Double 

當該斷點被擊中,然後將鼠標懸停root,看看是否爲空或不是。

如果是,則getDocument()中有空。

如果不是,則步驟過去這條線:

number = n.Count 

現在,將鼠標懸停在n,如果它爲空或不是。

,如果爲空,那麼這行:

n = root.SelectNodes("//playlist") 

有一個空引用的問題。


注:不管上面的原因,學習如何調試程序的關鍵是找到NullReferenceException S和任何其他異常爲這一問題。

+0

經過root和n之後,兩者都是xml文檔的內容......但是the_list沒有任何內容,我不明白爲什麼。 –

+0

所以,當單步執行代碼時,哪一行會拋出異常? –

+0

當它返回時我得到異常 –

相關問題