空重新引用VB(newbeginner)

时间:2015-06-02 09:25:10

标签: vb.net

我制作了以下代码,循环遍历XML文件的一些更改 当我运行代码时,我收到了这个错误。我是VB的新手。

enter image description here

有人可以帮我解决吗?

这是我的代码

 Private Sub BT_Browse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BT_Browse.Click

        ' Set Folder PATH default
        FolderBrowserDialog1.SelectedPath = My.Computer.FileSystem.SpecialDirectories.MyDocuments

        ' Show the new folder button
        FolderBrowserDialog1.ShowNewFolderButton = True

        If FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            ' Get the full path to the file that selected by the user.
            Dim mySelFile As String = FolderBrowserDialog1.SelectedPath

            Dim intcount As Integer = Nothing



            ' Displays the full path of the file selected by the user in the box (TextBox)
            Tb_FilePath.Text = mySelFile

            'Displays the folder name (only) selected, the user"Subtlety, use the" IO.Path.GetFileName "on the path to a folder
            'To get the name of the target folder.
            'While "IO.Path.GetDirectoryName" would have shown you the folder path CONTAINING file
            'Targeted by the specified path as a parameter
            'MsgBox("Du har valt: " & IO.Path.GetFileName(mySelFile))


            If Not IO.Directory.GetFiles(mySelFile, "*.xml").Any() Then
                MsgBox("there are no XML files here")
                Application.Restart()
            End If

            For Each filename As String In IO.Directory.GetFiles(mySelFile, "*.xml")

                intcount = intcount + 1

                Dim analyse
                Dim exactContexts
                Dim subnode
                Dim repeated
                Dim realRepeated
                Dim tmpValue As String = ""

                analyse = CreateObject("Msxml2.DOMDocument.6.0")
                analyse.Load(filename)

                exactContexts = analyse.SelectNodes("//inContextExact")

                For i = 0 To exactContexts.Length - 1
                    subnode = exactContexts(i)
                    For Each att In subnode.Attributes
                        If att.Name = "words" Then
                            att.Value = "0"
                            Exit For
                        End If
                    Next att
                Next i

                repeated = analyse.SelectNodes("//crossFileRepeated")

                For i = 0 To repeated.Length - 1
                    subnode = repeated(i)
                    For Each att In subnode.Attributes
                        If att.Name = "words" Then
                            tmpValue = att.Value
                            att.Value = "0"
                            Exit For
                        End If
                    Next att

                    realRepeated = subnode.NextSibling
                    For Each att In realRepeated.Attributes
                        If att.Name = "words" Then
                            att.Value = Val(att.Value) + Val(tmpValue)
                            Exit For
                        End If
                    Next att
                Next i



                analyse.Save(filename)


            Next

            MsgBox("there are (" + intcount.ToString + ") modified files")


        Else

            'if the user has not selected a folder, it is a warning
            MsgBox("No Folder selected", MsgBoxStyle.Exclamation, "No selected folders")
        End If


    End Sub

2 个答案:

答案 0 :(得分:1)

如果没有下一个节点,XmlNode.NextSibling property会返回Nothing

所以你必须检查一下这个案子:

...
realRepeated = subnode.NextSibling
If Not realRepeated Is Nothing Then 
    For Each att In realRepeated.Attributes
...

答案 1 :(得分:-1)

subNode没有兄弟姐妹,这就是为什么realRepeated值为Nothing,这意味着它作为变量没有属性。只需将此范围更改为:

    If subnode.NextSibling IsNot Nothing Then
        realRepeated = subnode.NextSibling
        For Each att In realRepeated.Attributes
            If att.Name = "words" Then
                att.Value = Val(att.Value) + Val(tmpValue)
                Exit For
            End If
        Next att
    End If