Reputation: 31
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
Upvotes: 2
Views: 12755
Reputation: 624
Sub ReadUsers(), uses the locally defined variables for Username, Index and Password. Remove these lines from Sub ReadUsers().
Dim Username() As String
Dim Password() As String
Dim Index As Integer
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)
Dim Username As List(Of String)
C. Then you no longer need to Redim. Just:
Username.add(encrypt(Feild(0)))
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
Upvotes: 0
Reputation: 1227
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".
Upvotes: 0
Reputation: 4519
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
Upvotes: 1
Reputation: 6450
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.
Upvotes: 0