【问题标题】:ASP.net 2010 (VB) Object reference not set to an instance of an objectASP.net 2010 (VB) 对象引用未设置为对象的实例
【发布时间】:2013-05-03 16:17:52
【问题描述】:

大家早安,

我正在将 VS2010 与 VB 一起使用,并且试图在我的 Web 应用程序中进行 ping 测试。为了做到这一点并测试它是否有效,我简单地创建了一个按钮,当点击时应该 ping 指定的 IP 地址。

我相信按钮的代码应该可以正常工作。我唯一的问题是我的网页上出现以下错误消息...

System.NullReferenceException: Object reference not set to an instance of an object.

它在 cole 线上的错误...

Console.WriteLine("Address: {0}", vPingReply.Address)

我认为这是由于需要为 .Address 和 .Status 对象设置“属性”。我不太确定我是否正确添加了这些,因为我已经添加了一些属性,但是当我运行页面时我仍然遇到同样的问题?

有人可以看看和建议吗?

这是我的完整代码...

Imports Microsoft.VisualBasic
Imports System.Text
Imports System.Net.NetworkInformation
Imports System.Net.NetworkInformation.PingReply


Partial Class Ping
Inherits System.Web.UI.Page

Private mSend As PingReply

Private Property Send(p1 As String) As PingReply
    Get
        Return mSend
    End Get
    Set(value As PingReply)
        mSend = value
    End Set
End Property


Private mAddress As PingReply

Private Property Address(p2 As String) As PingReply
    Get
        Return mAddress
    End Get
    Set(value As PingReply)
        mAddress = value
    End Set
End Property

Private mStatus As PingReply

Private Property Status(p3 As String) As PingReply
    Get
        Return mStatus
    End Get
    Set(value As PingReply)
        mStatus = value
    End Set
End Property


Protected Sub btnPing_Click(sender As Object, e As System.EventArgs) Handles btnPing.Click
    Dim vPing As New Ping
    Dim vPingReply As PingReply = vPing.Send("xxx.xx.xxx.xx")
    Console.WriteLine("Address: {0}", vPingReply.Address)
    Console.WriteLine("Status: {0}", vPingReply.Status)
End Sub

End Class

非常感谢任何帮助。

贝蒂。

【问题讨论】:

  • 如果您在调试中检查“vPingReply.Address”的值,它是否设置为任何值?
  • @Jacooobley,我已经调试了页面和 vPing.Send 和 vPingReply 都 = 没有。

标签: asp.net vb.net visual-studio-2010 visual-studio visual-studio-2008


【解决方案1】:

如果 Status 不是 Success,您将无法访问 Address 属性的内容

Dim vPing As New Ping
Dim vPingReply As PingReply = vPing.Send("xxx.xx.xxx.xx")
if vPingReply.Status = IPStatus.Success Then
    Console.WriteLine("Address: {0}", vPingReply.Address)
End If
Console.WriteLine("Status: {0}", vPingReply.Status)

文档说

如果 Status 的值不是 Success,则不应使用这些值 由 RoundtripTime、Options 或 Buffer 属性返回。这 RoundtripTime 属性将返回零,Buffer 属性将 返回一个空数组,Options 属性将返回 null。

但我发现如果发送用于不存在的 IP 地址或 DNS 名称,地址属性也为空(VB 中为空)

但是,仔细查看您的代码,很明显在 btnPing_Click 方法内进行的所有调用都由您的 Ping 类处理,而不是由框架类 Ping 处理。并且您的类使用未正确初始化的变量。我建议从你的类中删除这些方法,或者只是用不同的东西重命名类。
另一个选项(不推荐)是这个

Private Property Send(p1 As String) As PingReply
    Get
        ' Specify the full namespace to remove ambiguity between this class and framework class
        Dim p = new System.Net.NetworkInformation.Ping()
        mSend = p.Send(p1)
        Return mSend
    End Get
    Set(value As PingReply)
        mSend = value
    End Set
End Property

Private Property Address() As String
    Get
        if mSend IsNot Nothing Then
            Return mSend.Address
        else
            Return string.Empty
        End If
    End Get
    ' Meaningless ???
    'Set(value As PingReply)
    '    mAddress = value
    'End Set
End Property

Private mStatus As PingReply

Private Property Status() As String
    Get
        if mSend IsNot Nothing Then
            Return mSend.Status.ToString()
        else
            Return string.Empty
        End If
    End Get
    ' Meaningless ???
    'Set(value As PingReply)
    '    mStatus = value
    'End Set
End Property

【讨论】:

  • 我已包含您对 If 条件的建议,但网页现在在新代码行“If vPingReply.Status = IPStatus.Success Then”上出现错误,并显示相同的错误消息“System.NullReferenceException:你调用的对象是空的'。看起来代码没有从我拥有的 IP 地址中获取任何信息。但是我知道这个盒子可以作为我们的开发盒子使用,我可以从开始菜单和 cmd 提示符成功地 ping 这个?
  • 您的班级名为 Ping。这意味着当您调用 vPing.Send 时,您调用的是类的 Send 属性,而不是框架 Ping 类的 Send 方法。您的属性返回 mSend 内部变量,但此变量未在任何地方初始化,因此,当您调用 Success 时,就像您编写 Nothing.Success
  • 非常感谢您的帮助。我已将类名更改为 PingDetails 和 aspx 页面上的 Inherits 部分。
  • 这就是我要走的路。
猜你喜欢
  • 2013-10-28
  • 1970-01-01
  • 1970-01-01
  • 2020-04-13
  • 2013-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多