vb.net Visual Basic:未将对象引用设置为对象的实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20829421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:17:26  来源:igfitidea点击:

visual basic: object reference not set to an instance of an object

.netvb.netobjectreference

提问by okojie ernest

i watched a video tutorial and saw the guy write and execute the code below and worked but when i try to compile mine it says "object reference not set to an instance of an object". i have tried several things to see if i can get what the problem is but to no avail.

我观看了一个视频教程,看到这个人编写并执行下面的代码并工作,但是当我尝试编译我的代码时,它说“对象引用未设置为对象的实例”。我已经尝试了几件事,看看我是否能找到问题所在,但无济于事。

Imports System.IO
Public Class Form2

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim myline = New StreamReader("TextFile1.txt")
            Dim line As String = ""
            While Not IsNothing(line)
                line = myline.ReadLine
                If IsNothing(line) Then
                    TextBox2.AppendText(line)
                End If
            End While
            myline.Close()

        Catch ex As Exception

            TextBox2.AppendText(ex.Message)
            MsgBox(ex.Message)

        End Try


    End Sub

End Class

can anyone help please? thanks

有人可以帮忙吗?谢谢

回答by Markus

I think you are missing a Not:

我认为你缺少一个Not

            If Not IsNothing(line) Then
                TextBox2.AppendText(line)
            End If

回答by ??ssa P?ngj?rdenlarp

this kind of "If Not IsNothing:" is strange kind of.

this kind of "If Not IsNothing:" is strange kind of.

IsNothingis a VB-ism as opposed to NET syntax. Other ways of coding it include:

IsNothing是一种 VB 主义,而不是 NET 语法。其他编码方式包括:

If String(line).IsNullOrEmpty = False Then 
' or 
If Not String(line).IsNullOrEmpty Then 
' which is the same type of garble as Not IsNothing

also:
If Line IsNot Nothing Then

Since IsNothingis a VB function returning a Boolean, you can also just evaluate it:

由于IsNothing是返回布尔值的 VB 函数,您也可以对其进行评估:

If IsNothing(line) = False Then ...

回答by Raheel Khan

In the condition If IsNothing(line) Thenyou are missing a Not.

If IsNothing(line) Then您缺少Not.

If Not IsNothing(line) Then
    TextBox2.AppendText(line)
End If

回答by TheVolatileChemist

Well, it's simple enough :) Just missing your object definition :)

嗯,这很简单:) 只是缺少您的对象定义:)

Dim myline = New StreamReader("TextFile1.txt")

-doesnt define myline. It's like your doing this:

- 没有定义 myline。这就像你这样做:

Dim variable = 1

You have to say:

你不得不说:

Dim myline as StreamReader = New StreamReader("TextFile1.txt")

So that Visual Basic knows what kind of variable you're making before you stuff it with a value. There are better, more acute ways of reading files in VB.NET (Personally my favorite language :) ), but I'm sure this will do for your application.

这样 Visual Basic 就知道在用值填充变量之前要创建什么样的变量。在 VB.NET(我个人最喜欢的语言:))中有更好、更敏锐的读取文件的方法,但我相信这对您的应用程序有用。

If you end up wanting to read an entire file to an array (list), you can look through snippets to find the code for reading a file to a string, then split it to a list with the anystring.split command and the "vbCRLF" character (An endline, like your enter button).

如果您最终想要将整个文件读取到数组(列表)中,您可以查看片段以找到将文件读取为字符串的代码,然后使用 anystring.split 命令和“vbCRLF”将其拆分为列表" 字符(结束线,就像您的输入按钮)。

回答by Chris Dunaway

Seems like it would be easier to just call the ReadAllText method:

似乎只调用 ReadAllText 方法会更容易:

Imports System.IO

Public Class Form2

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            TextBox2.AppendText(File.ReadAllText("TextFile1.txt"))
        Catch ex As Exception
            TextBox2.AppendText(ex.Message)
            MsgBox(ex.Message)
        End Try
    End Sub

End Class