【问题标题】:VB.net null reference exception: object reference not set to an instance of an objectVB.net空引用异常:对象引用未设置为对象的实例
【发布时间】:2013-12-30 03:45:55
【问题描述】:

我正在开发一个在用户之间共享播放列表的项目,我正在使用 XML 文档“playlist.xml”。

以下是给我空引用异常的代码

<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:对象引用未设置为对象的实例。 在 C:\Users\jamie\Dropbox\UNIVERSITY\WebSite\App_Code\Service.vb 中的 Service.getList(String listNick):第 36 行 -- 我发布的代码的第 11 行

这是大学的课程。

【问题讨论】:

    标签: vb.net xpath nullreferenceexception


    【解决方案1】:

    我的猜测是 rootn 为空。

    通过在以下行放置一个断点来验证这一点:

    Dim number As Double
    

    当遇到该断点时,将鼠标悬停在root 上并查看是否为空。

    如果是,那么getDocument() 中有一个空值。

    如果没有,请跳过这一行:

    number = n.Count
    

    现在,将鼠标悬停在 n 上,看看它是否为空。

    如果为null,那么这一行:

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

    有一个空引用问题。


    注意:无论上述原因如何,了解如何调试程序是找到NullReferenceExceptions 和任何其他与此相关的异常的关键。

    【讨论】:

    • 在越过 root 和 n 之后,两者都是 xml 文档的内容...但是 the_list 什么都没有,我不明白为什么。
    • 那么在单步执行代码时,它会在哪一行引发异常?
    • 当它返回时我得到了异常
    • 你没有贴出调用web服务方法的代码,那个代码在哪里?
    • 我正在使用内置的网络客户端,它是我浏览器上的一个网页,会询问我任何参数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    • 2013-06-14
    • 2013-01-14
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多