-1

I have made following code that loops through XML files some changes when I run the code I got this error. I am newbeginner to VB.

enter image description here

Could someone help me to solve it?

here is my code

 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
Brian Hooper
  • 21,544
  • 24
  • 88
  • 139
XsiSecOfficial
  • 954
  • 8
  • 20

2 Answers2

1

The XmlNode.NextSibling property returns Nothing if there is no next node.

So you have to simply check for this case:

...
realRepeated = subnode.NextSibling
If Not realRepeated Is Nothing Then 
    For Each att In realRepeated.Attributes
...
sloth
  • 99,095
  • 21
  • 171
  • 219
-1

subNode has not siblings that's why realRepeated value is Nothing which means as a variable it has no attributes. Just change this scope to:

    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
Saleem
  • 709
  • 2
  • 13
  • 34