Visual Basic - Object reference not set to an instance of an object
11,622
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
Author by
Admin
Updated on June 04, 2022Comments
-
Admin almost 2 years
I'm receiving the following error in VB.Net.
"Object reference not set to an instance of an object"
It highlights "Next" at the end of the For Loop.
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
-
David Brunelle over 14 yearsWhat is the content of your data string?