类型' System.ArgumentNullException'的未处理异常发生在System.dll中

时间:2016-01-15 11:05:08

标签: vb.net

所以我用VB.NET编写了这段代码 当它到达" Dim ping As New Ping"它会抛出错误"未处理的类型' System.ArgumentNullException'发生在System.dll

附加信息:值不能为空。"

http://pastebin.com/C1nfdzUN

Imports System.Net.NetworkInformation

Public Class Form1
    Dim i As Boolean
    Dim pingMs As String


    'I'm using this in the createTextIcon sub to releases all of the resources that the icon/icons would have used.
    Public Declare Function DestroyIcon Lib "user32.dll" (ByVal hIcon As Int32) As Int32

    'You should fine tune the font you want to use so the user can see the text you want them to see.
    'Certain Fonts will obviously display your text better than other fonts might.
    Dim fontToUse As Font = New Font("Microsoft Sans Serif", 8, FontStyle.Regular, GraphicsUnit.Pixel)

    'A basic brush with a Dark Blue Color. This should show up pretty well in the icon tray if the user uses the default tray color.
    Dim brushToUse As Brush = New SolidBrush(Color.DarkBlue)

    'A bitmap used to setup how the icon will display.
    Dim bitmapText As Bitmap = New Bitmap(16, 16)

    'A simply Grahics object to draw the text to a bitmap.
    Dim g As Graphics = Drawing.Graphics.FromImage(bitmapText)

    'Will be used to get the Handle to the bitmap Icon.
    Dim hIcon As IntPtr

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Application.DoEvents()
        Threading.Thread.Sleep(10)
        Dim pingTarget As String = ""
        Dim numberOfPings As Integer = 0
        Dim ping As New Ping
        Dim pingRe As PingReply = ping.Send(pingTarget, 1)
        Dim o As Boolean = True
        Dim delay As Integer = 1

        pingTarget = "www.google.com"
        i = True

        While i
            pingMs = pingRe.RoundtripTime.ToString
            Label1.Text = pingMs
            createTextIcon()

            Application.DoEvents()


            Do Until o = False
                Application.DoEvents()
                Threading.Thread.Sleep(10)
                delay = delay + 1
                If delay = 100 Then
                    o = False
                    delay = 1
                End If
            Loop
            o = True
        End While
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        i = False

        Application.DoEvents()
        Close()
    End Sub

    Sub createTextIcon()
            'Clear any previous ‘stuff’ instead of creating a new bitmap.
            g.Clear(Color.Transparent)

            'Setup the text, font, brush, and position for the system tray icon. For the font type and
            'size I used, a good position for the X coordinate is a -1 or -2. And the Y coordinate seems
            'to work well at a 5.
            'You specify the actual text you want to be displayed in the draw string parameter that you
            'want to display in the notify area of the system tray. You will only be able to display a
            'few characters, depending on the font, size of the font, and the coordinates you used.
            g.DrawString(pingMs, fontToUse, brushToUse, -2, 5)

            'Get a handle to the bitmap as a Icon.
            hIcon = (bitmapText.GetHicon)

            'Display that new usage value image in the system tray.
            NotifyText.Icon = Drawing.Icon.FromHandle(hIcon)

            'Added this to try and get away from a rare Generic Error from the code above. Using this API Function seems to have stopped that error from happening.
            DestroyIcon(hIcon.ToInt32)

    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

您从ArgumentNullException获得ping.Send(),因为当您调用该方法时pingTarget为空字符串。
移动线

pingTarget = "www.google.com"

以上ping.Send()