【问题标题】:An unhandled exception of type 'System.ArgumentNullException' occurred in System.dllSystem.dll 中出现“System.ArgumentNullException”类型的未处理异常
【发布时间】:2016-01-15 11:05:07
【问题描述】:

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

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

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

【问题讨论】:

  • 您在调用 .Send 后设置 pingTarget。

标签: vb.net


【解决方案1】:

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

pingTarget = "www.google.com"

高于ping.Send()。

【讨论】:

    猜你喜欢
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-26
    相关资源
    最近更新 更多