调试错误:“对象引用未设置为对象的实例”

时间:2015-11-21 18:43:12

标签: vb.net

简介(警告:英语不好)

各位大家好。我的程序需要一些帮助。

因此,基本上我的程序用于下载一个文件并将其提取到已知路径,例如C:\Users\ProfileName\Documents\Windward。这条路径适合大多数人。但是对于某些人来说这是错误的路径(因为文件需要安装在Documents\Windward文件夹中)。所以我决定改变路径。我以为我把一切都做对了,但出了点问题。我觉得有些事情是这样的:Dim path As String = TextBox1.Text & "\Localization.zip",但我不知道如何修复它。

请帮助我!

错误:

    An unhandled exception of type System.InvalidOperationException occurred in AutoDownloadV2.exe

    Additional information: Error in form creating.  Exception.InnerException. Error: Object reference not set to an instance of an object.

调试后我收到了这个:

in AutoDownloadV2.My.MyProject.MyForms.Create__Instance__[T](T Instance) in :string 190

in AutoDownloadV2.My.MyApplication.OnCreateMainForm() в B:\Projects\Progs\AutoDownloadV2\AutoDownloadV2\AutoDownloadV2\My Project\Application.Designer.vb:string 35

in Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()

in Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()

in Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)

in AutoDownloadV2.My.MyApplication.Main(String[] Args) in :string 81 The program '[5452] AutoDownloadV2.exe' has exited with code 0 (0x0).

这是我的代码(我删除了无用的代码部分。)

Public Class Form1

Public WithEvents download As WebClient
Dim path As String = TextBox1.Text & "\Localization.zip"

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    TextBox1.Text = "C:\Users\" & SystemInformation.UserName & "\Documents\Windward"
    ProgressBar1.Value = 0
    CheckForIllegalCrossThreadCalls = False 
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Try
        Try
            My.Computer.FileSystem.DeleteFile(path)
        Catch ex As Exception
        End Try
        download = New WebClient
        download.DownloadFileAsync(New Uri("http://http://exsite.example"), path)
    Catch ex As Exception
        MsgBox("Error! " & ex.Message)
    End Try
End Sub

Private Sub download_DownloadProgressChanged(ByVal sender As System.Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles download.DownloadProgressChanged
    Try
        Label3.Text = "Downloaded : " & e.BytesReceived / 1024 & " kb / " & e.TotalBytesToReceive / 1024 & " kb  "
        Label4.Text = ProgressBar1.Value & "%"
        ProgressBar1.Value = e.ProgressPercentage
    Catch ex As Exception
        MsgBox("Error! " & ex.Message)
    End Try
    If e.BytesReceived = e.TotalBytesToReceive Then
        Unzip()
    End If
End Sub

Public Sub Unzip()
    Dim startPath As String = path
    Dim zipPath As String = path
    Dim extractPath As String = ("C:\Users\" + SystemInformation.UserName + "\Documents\Windward\")
    If My.Computer.FileSystem.FileExists(extractPath + "mods\translateMod\Localization.txt") Then
        My.Computer.FileSystem.DeleteFile(extractPath + "mods\translateMod\Localization.txt")
        Try
            My.Computer.FileSystem.DeleteFile(extractPath + "mods\translateMod\Version.txt")
        Catch e As Exception
        End Try
        ZipFile.ExtractToDirectory(zipPath, extractPath)
        My.Computer.FileSystem.DeleteFile(path)
    Else
        ZipFile.ExtractToDirectory(zipPath, extractPath)
        My.Computer.FileSystem.DeleteFile(path)
    End If
End Sub
End Class

1 个答案:

答案 0 :(得分:0)

在TextBox1

中设置初始值后,在Form1_Load中移动路径初始化
Dim path As String 

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    TextBox1.Text = "C:\Users\" & SystemInformation.UserName & "\Documents\Windward"
    path = TextBox1.Text & "\Localization.zip"
    ProgressBar1.Value = 0
    CheckForIllegalCrossThreadCalls = False 
End Sub

问题是由表单类的全局区域中对TextBox1的引用引起的。此时TextBox1仍为Nothing(未初始化),因此您获得NRE消息(Null Reference Exception)