Tuesday 9 April 2019

vb.net - Visual Basic - Object reference not set to an instance of an object

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

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...