我在VB.Net中收到以下错误。
“对象引用未设置为对象的实例”
它突出显示For循环结束时的“下一步”。
任何帮助都会很棒。
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
答案 0 :(得分:1)
尝试替换此代码:
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
使用此代码:
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
答案 1 :(得分:0)
您接下来要参考哪个?
在你的第二个中,定义那个是I.这可能无法解决问题,但这绝对是一个更好的实践。
您的数据是否可能构成一个'null'字符(chr(0))?
如果Mid到达字符串的末尾,则返回null,但看起来不会发生这种情况。
尽管如此,您可能希望使用String.Substring而不是mid。这是一个用字符串对象找到的函数。
答案 2 :(得分:0)
我会猜测这是在LoginButton_Click里面的“For Each I In Username”循环导致你出现问题吗?
我猜这个循环因为变量“I”的类型似乎没有被声明,所以它默认是Object类型,匹配错误“Object reference not set to a object of object”。
答案 3 :(得分:0)
Sub ReadUsers()使用本地定义的变量用于用户名,索引和密码。从Sub ReadUsers()中删除这些行。
Dim Username() As String
Dim Password() As String
Dim Index As Integer
在班级。
一个。将此导入添加到文件的顶部:
Imports System.Collections.Generic
B中。将String数组定义更改为List(of String)
Dim Username As List(Of String)
℃。然后你不再需要Redim了。只是:
Username.add(encrypt(Feild(0)))
循环计数而不是项目:
For i as integer = 0 to Username.length - 1
If UserNameTextBox.Text = Username(i) Then
...
Next
最后,这是你的代码:
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