1

我正在尝试从文件中下载字符串,但收到以下警告

警告 BC42104:变量“inst”在被赋值之前被使用。运行时可能会导致空引用异常。

这是我的代码

Dim inst As WebClient
        Dim inst2 As WebClient
        Dim inst3 As WebClient
        Try
            MsgBox("started")
            ver = inst.DownloadString("http://www.xxxxxxxxx.com/update/version.xml")
            loc = inst2.DownloadString("http://www.xxxxxxxxx.com/update/loc.xml")
            desc = inst3.DownloadString("http://www.xxxxxxxxx.com/update/description.xml")
            If (String.Compare(ver, String.Format(Nothing, My.Application.Info.Version.Major.ToString) + "." + String.Format(Nothing, My.Application.Info.Version.Minor.ToString)) = False) Then
                updreq = True
            End If
        Catch ex As Exception
            MessageBox.Show("Error occured: " + ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
4

2 回答 2

1

该代码肯定会导致空引用异常。你已经声明了变量来保存WebClient对象,但是你还没有创建任何实际的WebClient实例。

WebClient为变量创建类的实例:

Dim inst As WebClient = New WebClient()

或简写:

Dim inst As New WebClient()
于 2013-07-07T20:04:25.113 回答
-1

我有类似的情况,我做了与上面相同的操作,但对于 TabPage:

Private Sub btnAddTab_Click(sender As Object, e As EventArgs) Handles btnAddTab.Click
    Dim number As Integer = TabControl1.TabPages.Count
    Dim tab As TabPage = New TabPage()
    tab.Text = "TabPage" & number + 1
    TabControl1.TabPages.Add(tab)
    tab.BackColor = Color.DarkGreen
End Sub
于 2017-01-23T18:08:15.747 回答