2016-12-06 54 views
-1

這是我的代碼的Visual Basic。載有()空異常

  Dim words(0) As String 
      Dim trie As String 
      Dim temp As String 
      For i = 1 To 26 
       trie = encrypt(a, -i) 
       Console.WriteLine(trie) 
       For j = 0 To words.Length - 1 
        temp = words(j) 
        If trie.Contains(temp) Then 
         Console.ReadLine() 
        End If 
       Next 
      Next 

應該檢查是否線索包含數組的話任何項目,但它拋出一個異常NULL

encrypt(a, -i)只是改變字符串

+0

細節的缺乏。什麼是'了'? –

+0

什麼是例外? –

+0

['String.Contains'](https://msdn.microsoft.com/de-de/library/dy85x1sa(v = vs.110).aspx)如果傳遞的參數是空引用,則會引發'ArgumentNullException'。所以你必須檢查它:'如果溫度不是Nothing並且trie.Contains(temp)Then ...' –

回答

0

你必須檢查是否這些變量是空的字母:

Dim words(0) As String = String.Empty 
Dim trie As String = String.Empty 
Dim temp As String = String.Empty 

For i = 1 To 26 
    trie = encrypt(a, -i) 
    Console.WriteLine(trie) 
    For j = 0 To words.Length - 1 
     temp = words(j) 
     If Not temp Is Nothing AndAlso _ 
      Not trie Is Nothing AndAlso _ 
       trie.Contains(temp) Then 
       Console.ReadLine() 
     End If 
    Next 
Next