对象引用未设置为vb.net中对象的实例

对象引用未设置为vb.net中对象的实例,vb.net,object,reference,Vb.net,Object,Reference,我得到一个错误: 对象引用未设置为对象的实例 当我试图将任何文件上载到数据库时,会出现此问题。这是我正在为我的学校(期末考试)开发的项目 这是我的密码: Try OpenFileDialog.Filter = "All files (*.*)|*.*| JPEG FILES(*.jpg)|*.jpg| RAW FILES (*.raw)|*.raw| WORD FILES (*.doc)|*.doc| WORD FILES (*.docx)|*.docx| EXCEL FILE

我得到一个错误:

对象引用未设置为对象的实例

当我试图将任何文件上载到数据库时,会出现此问题。这是我正在为我的学校(期末考试)开发的项目

这是我的密码:

 Try
        OpenFileDialog.Filter = "All files (*.*)|*.*| JPEG FILES(*.jpg)|*.jpg| RAW FILES (*.raw)|*.raw| WORD FILES (*.doc)|*.doc| WORD FILES (*.docx)|*.docx| EXCEL FILES (*.xls)|*.xls| EXCEL Files (*.xlsx)|*.xlsx| PNG Files (*.png)|*.png| GIF FILES (*.gif)|*.gif| PDF FILES (*.pdf)|*.pdf| RAR Files (*.rar)|*.rar| AUTOCAD FILES (*.dwg)|*.dwg| EXE FILES (*.exe)|*.exe| ZIP FILES (*.zip)|*.zip| TXT FILES (*.txt)|*.txt"
        OpenFileDialog.FilterIndex = 1
        OpenFileDialog.Title = "Open File"
        OpenFileDialog.Multiselect = True

        obj_openfile = OpenFileDialog

        'Show The Dialog
        If obj_openfile.ShowDialog = Windows.Forms.DialogResult.OK Then
            in_file = obj_openfile.FileName.ToString
            Dim fich As String = obj_openfile.SafeFileName
            obj_openfile.Dispose()
            obj_openfile = Nothing
            con.Close()
            con.ConnectionString = "Data Source=IZZ;Initial Catalog=regteste;Integrated Security=True"
            con.Open()

            'Converter to bytesream
            Dim fs As FileStream
            fs = New FileStream(in_file, FileMode.Open, FileAccess.Read)
            Dim docByte As Byte() = New Byte(fs.Length - 1) {}
            fs.Read(docByte, 0, System.Convert.ToInt32(fs.Length))
            fs.Close()

            Dim filebyte As Byte() = Nothing
            filebyte = System.IO.File.ReadAllBytes(obj_openfile.FileName)
            cmd = New SqlCommand("INSERT INTO Filestelefones_pt (Name,Data) VALUES (@Name, @Data)", con)
            cmd.Parameters.AddWithValue("@Name", fich)
            cmd.Parameters.AddWithValue("@Data", docByte)
            cmd.ExecuteNonQuery()
            con.Close()
            con.Open()

            txtabrir.Text = in_file

        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

找到这些问题的最简单方法是在代码中放置一个断点,点击F5并在调试器中逐行检查。这也比等待回答要快

你的代码中最可能的罪魁祸首是
obj\u openfile

如果查看
If
中的代码,您会注意到以下代码行:

obj_openfile.Dispose()
obj_openfile = Nothing
再往下一点,你会看到:

Dim filebyte As Byte() = Nothing
filebyte = System.IO.File.ReadAllBytes(obj_openfile.FileName)

在程序中的这一点上,您已经将
obj_openfile
设置为
Nothing
(null),因此这很可能是您遇到错误的地方,因为您正在尝试读取null对象的属性。

您应该添加一个语言标记。您知道它在哪里失败吗?你能提供更多关于错误的详细信息吗?可能的重复开始时,去掉try/catch以确定错误的确切位置。是的,Puropolix是相同的,但我是这类comunity的新手。谢谢;)非常感谢你的帮助。这是当场的。:)