简体   繁体   English

Visual Basic-对象引用未设置为对象的实例

[英]Visual Basic - Object reference not set to an instance of an object

I'm receiving the following error in VB.Net. 我在VB.Net中收到以下错误。

"Object reference not set to an instance of an object" “你调用的对象是空的”

It highlights "Next" at the end of the For Loop. 它在For循环的末尾突出显示“下一步”。

Any help would be great. 任何帮助都会很棒。

Imports System.IO
Public Class LoginForm
    Dim Username() As String
    Dim Password() As String
    Dim Index As Integer

    Public Function encrypt(ByVal data As String) As String
        Dim answer As String = ""
        Dim I As Integer
        data = RTrim(data)
        If Mid(data, 1, 1) <> Chr(0) Then
            For I = 1 To Len(data)
                answer = answer + Chr(Asc(Mid(data, I, 1)) Xor 23) 
               ' Xor 23 is a simple encription cipher, a string can be 
               ' encrypted or de-encrypted by the value following the Xor 
               'i.e. "23" '
            Next I
        End If
        encrypt = answer
    End Function

    Private Sub LoginButton_Click(ByVal sender As System.Object, _
                                    ByVal e As System.EventArgs) _
                                Handles LoginButton.Click

        For Each I In Username

            If UserNameTextBox.Text = Username(Index) Then
                UserAdd.Show()
                Me.Hide()
                If PasswordTextBox.Text = Password(Index) Then
                    MessageBox.Show("Correct Password")
                Else
                    MessageBox.Show("Invalid Password, Sorry")
                End If
            Else : MessageBox.Show("Invalid Username, Sorry")
            End If    
        Next    
    End Sub

    Public Sub ReadUsers()
        Dim CurrentFileReader As StreamReader
        Dim FileName, Line As String
        Dim Delimiter As Char = ","
        Dim Feild() As String
        Dim Username() As String
        Dim Password() As String
        Dim Index As Integer

        FileName = "C:\Computing\Projects\Login\Users.txt"  'location of 
                                                            'user file 
        CurrentFileReader = New StreamReader(FileName)

        Do Until CurrentFileReader.EndOfStream

            Line = CurrentFileReader.ReadLine
            If Line = Nothing Then
                Exit Do
            End If

            ReDim Preserve Username(Index)
            ReDim Preserve Password(Index)

            Feild = Line.Split(Delimiter)

            Username(Index) = encrypt(Feild(0))
            Password(Index) = encrypt(Feild(1))        
        Loop
    End Sub        

    Private Sub LoginForm_Load(ByVal sender As Object, _
                                ByVal e As System.EventArgs) _
                            Handles Me.Load
        Call ReadUsers()
    End Sub
End Class

Try replacing this code: 尝试替换此代码:

For Each I In Username 

            If UserNameTextBox.Text = Username(Index) Then 
                UserAdd.Show() 
                Me.Hide() 
                If PasswordTextBox.Text = Password(Index) Then 
                    MessageBox.Show("Correct Password") 
                Else 
                    MessageBox.Show("Invalid Password, Sorry") 
                End If 
            Else : MessageBox.Show("Invalid Username, Sorry") 
            End If 

Next 

with this code: 使用此代码:

For Each I In Username 

      if Username(i) is not null then

            If UserNameTextBox.Text = Username(Index) Then 
                UserAdd.Show() 
                Me.Hide() 
                If PasswordTextBox.Text = Password(Index) Then 
                    MessageBox.Show("Correct Password") 
                Else 
                    MessageBox.Show("Invalid Password, Sorry") 
                End If 
            Else : MessageBox.Show("Invalid Username, Sorry") 
            End If 
      else
            ....handle empty string
      end if

        Next 

Which next are yout refering to? 您要指的是下一个?

In your second for, define that is I. That may not solve the problem, but this is definitly a better practive. 在您的第二篇文章中,将其定义为I。这可能无法解决问题,但绝对是一个更好的方法。

Is it possible that your data constains a 'null' character (chr(0))? 您的数据是否可能包含“空”字符(chr(0))?

Mid will return null if it reaches the end of the string , but it doesn't look like this would happen to you. 如果Mid到达字符串的末尾,它将返回null,但是看起来这不会发生在您身上。

Nevertheless, you might want to use String.Substring instead of mid. 但是,您可能想要使用String.Substring而不是mid。 It's a function found with the string object. 这是在字符串对象中找到的函数。

I'll take a guess that it's the "For Each I In Username" loop inside LoginButton_Click that's causing you the problem? 我会猜测是LoginButton_Click内部的“ For Each I Username”循环导致了您的问题?

I'm guessing at this loop as the type of variable "I" does not appear to be declared, so it would be type Object by default, matching the error "Object reference not set to an instance of an object". 我猜在这个循环中,因为似乎没有声明变量“ I”的类型,因此默认情况下它将是Object类型,与错误“ Object reference not set to an object instance”匹配。

  1. Sub ReadUsers(), uses the locally defined variables for Username, Index and Password. Sub ReadUsers(),将本地定义的变量用于用户名,索引和密码。 Remove these lines from Sub ReadUsers(). 从Sub ReadUsers()中删除这些行。

     Dim Username() As String Dim Password() As String Dim Index As Integer 
  2. At your class level. 在您的课堂上。

    A. Add this Imports to the top of the file: 答:将此导入添加到文件的顶部:

      Imports System.Collections.Generic 

    B. Change your String array definitions to List(of String) B.将您的字符串数组定义更改为List(of String)

      Dim Username As List(Of String) 

    C. Then you no longer need to Redim. C.然后您不再需要Redim。 Just: 只是:

      Username.add(encrypt(Feild(0))) 
  3. Loop on the count instead of item: 循环计数而不是计数:

      For i as integer = 0 to Username.length - 1 If UserNameTextBox.Text = Username(i) Then ... Next 

And finally, here's your code: 最后,这是您的代码:

Imports System.IO
Imports System.Collections.Generic
Public Class LoginForm

    ' At the Class level Dim is equivalent to Private
    Private Username As List(Of String)
    Private Password As List(Of String)
    Private Index As Integer

    Public Function encrypt(ByVal data As String) As String
        Dim answer As String = ""
        Dim I As Integer
        data = RTrim(data)
        If Mid(data, 1, 1) <> Chr(0) Then
            For I = 1 To Len(data)
                answer = answer + Chr(Asc(Mid(data, I, 1)) Xor 23)
                ' Xor 23 is a simple encription cipher, a string can be 
                ' encrypted or de-encrypted by the value following the Xor 
                'i.e. "23" '
            Next I
        End If
        encrypt = answer
    End Function

    Private Sub LoginButton_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles LoginButton.Click

        For i As Integer = 0 To Username.length - 1

            If UserNameTextBox.Text = Username(i) Then
                UserAdd.Show()
                Me.Hide()
                If PasswordTextBox.Text = Password(i) Then
                    MessageBox.Show("Correct Password")
                Else
                    MessageBox.Show("Invalid Password, Sorry")
                End If
            Else : MessageBox.Show("Invalid Username, Sorry")
            End If
        Next
    End Sub

    Public Sub ReadUsers()
        Dim CurrentFileReader As StreamReader
        Dim FileName, Line As String
        Dim Delimiter As Char = ","
        Dim Feild() As String


        FileName = "C:\Computing\Projects\Login\Users.txt"  'location of 
        'user file 
        CurrentFileReader = New StreamReader(FileName)

        Do Until CurrentFileReader.EndOfStream

            Line = CurrentFileReader.ReadLine
            If Line = Nothing Then
                Exit Do
            End If

            Feild = Line.Split(Delimiter)

            Username.Add(encrypt(Feild(0)))
            Password.add(encrypt(Feild(1)))
        Loop
    End Sub

    Private Sub LoginForm_Load(ByVal sender As Object, _
                                ByVal e As System.EventArgs) _
                            Handles Me.Load
        Call ReadUsers()
    End Sub
    End Class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM