0

大家早,

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

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

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

它在油菜线上出问题了...

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

任何帮助都是非常重要的。

贝蒂。

4

1 回答 1

0

如果 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
于 2013-05-09T08:36:52.110 回答