この「System.IndexOutOfRangeException」がどのように発生する可能性があるかを解読できる人はいますか? コードを調べましたが、作成したメソッドが例外をキャッチすると、「Nothing」という単語を含む文字列を返すため、文字列を空にすることは不可能です。
例外が発生する場所は次のとおりです。
Do While (Not (nodeStep Is Nothing))
stepCmd = cmd + (XPathCommand.STEPCMD + ("/tr[" + stepCount.ToString + "]"))
stepString = htmlManager.GetTextbyNode(htmlManager.GetNodebyXPath(stepCmd))
stepChar = stepString.Chars(0) '<--- Exception thrown here!
Select Case stepChar
Case "D"
testScript.StepDesc.Add(stepString.Replace("Description: ", ""))
Case "E"
testScript.StepExpect.Add(stepString.Replace("Expected: ", ""))
Case "S"
If Not (testScript.StepDesc.Count = testScript.StepExpect.Count) Then
testScript.StepExpect.Add("< No expected step here >")
End If
End Select
stepCount += 1
nodeStep = htmlManager.GetNodebyXPath(cmd + (XPathCommand.STEPCMD + ("/tr[" + stepCount.ToString + "]")))
Loop
これで、htmlManager.GetTextbyNode メソッド自体が HtmlAgilityPack.HtmlNode を受け取り、最初にテキスト文字列をクリーンアップした後、ノード内のすべてのテキストを返します。問題は、メソッドが (テキストを持たないノードに対して) キャッチオールを持ち、文字列 "Nothing" を返すことです。
Function GetTextbyNode(ByVal node As HtmlAgilityPack.HtmlNode) As String
Try
Return StringCleaner.Clean(node.InnerText)
Catch
'If find a NULL Text node
End Try
Return "Nothing"
End Function
渡されたノードに NULL テキストがあるかどうかにかかわらず、有効な文字列が返されるはずですよね? そして、有効な文字列を取得している場合、String.Chars(0) には少なくとも文字が必要ですか? NULL 文字列を返すべきではありませんよね?または、何か不足していますか?