ASP.net 2010(VB)对象引用未设置为对象的实例

时间:2013-05-09 08:28:53

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

早上好,

我正在使用VS2010和我试图在我的Web应用程序中进行ping测试。为了做到这一点并测试它的工作原理我只是创建了一个按钮,当点击时应该ping一个指定的IP地址。

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

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

它在油菜线上出错...

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

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

有人可以看看并提出建议吗?

这是我的完整代码......

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

任何帮助都很有用。

贝蒂。

1 个答案:

答案 0 :(得分:0)

如果“状态不是成功”

,则无法访问“地址”属性的内容
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名称,则Address属性为null(VB中为Nothing)

但是,在查看代码时,很明显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