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
I'll take a guess that it's the "For Each I In Username" loop inside LoginButton_Click that's causing you the problem?
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".
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.
Is it possible that your data constains a 'null' character (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.
Nevertheless, you might want to use String.Substring instead of mid. It's a function found with the string object.
Sub ReadUsers(), uses the locally defined variables for Username, Index and Password. Remove these lines from Sub ReadUsers().
At your class level.
A. Add this Imports to the top of the file:
B. Change your String array definitions to List(of String)
C. Then you no longer need to Redim. Just:
Loop on the count instead of item:
And finally, here's your code:
Try replacing this code:
with this code: