2009-12-07 98 views
2

我在VB.Net中收到以下錯誤。Visual Basic - 未將對象引用設置爲對象的實例

「對象引用不設置爲一個對象的一個​​實例」

它突出「下一步」的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 
+1

有一個以上的在循環您碼。哪一個拋出異常?它是在第一遍還是以後發生?您是否應該檢查集合中是否有零個以上的項目? – DOK 2009-12-07 13:52:58

+1

以「Next」結尾的第一個循環 – 2009-12-07 14:18:57

回答

0

下一個是你指的是哪一個?

在你的第二個問題中,將其定義爲I.這可能無法解決問題,但這確實是一個更好的實踐。

你的數據是否有可能產生'null'字符(chr(0))?

如果mid到達字符串的末尾,Mid將返回null,但它看起來不會發生在您身上。

不過,你可能想使用String.Substring而不是mid。這是一個在字符串對象中找到的函數。

+0

String.Substring也不起作用。 – 2009-12-07 15:34:35

+0

你的數據字符串的內容是什麼? – 2009-12-07 16:22:01

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 
0

我要猜測,它是「對於每一個我在用戶名」內LoginButton_Click循環是造成你的問題?

我在這個循環中猜測變量「I」的類型沒有被聲明,因此默認情況下它會是類型Object,匹配錯誤「Object reference not set to a instance of a object」 。

0
  1. Sub ReadUsers(),使用本地定義的變量作爲用戶名,索引和密碼。從Sub ReadUsers()中刪除這些行。

    Dim Username() As String 
    Dim Password() As String 
    Dim Index As Integer 
    
  2. 在你的課堂上。

    A.添加此導入到文件的頂部:

    Imports System.Collections.Generic 
    

    B.改變你的字符串數組定義,以列表(一個String)

    Dim Username As List(Of String) 
    

    。然後您不再需要去Redim。剛:

    Username.add(encrypt(Feild(0))) 
    
  3. 循環的次數,而不是項目:

    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 
相關問題