Question [2008] Problem with Nested or Multidimensional Data Structures

mecablaze

Member
Joined
Oct 4, 2008
Messages
5
Programming Experience
1-3
Hello,

Right now I'm working on parsing an XML file and storing the data I need into some form of data structure to be used in by VB.net program. Here's my code:

VB.NET:
Expand Collapse Copy
Imports System.ComponentModel
Imports System.Xml


Public Class getPackageDataForm
    Public defaultQueue As Queue(Of String())
    Public otherQueue As Queue(Of String())
    Public linksQueue As Queue(Of String())

    Private cancelled As Boolean

    Private Sub xmlProcess_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        BackgroundWorker1.ReportProgress(0, "(1/3) Loading XML")
        Dim reader As XmlTextReader = New XmlTextReader( (this is where I put my xml URL) )

        BackgroundWorker1.ReportProgress(0, "(2/3) Parsing XML")

        Dim defaultBoolean As Boolean = False
        Dim otherBoolean As Boolean = False
        Dim linksBoolean As Boolean = False

        While (reader.Read())
            Select Case reader.NodeType
                Case XmlNodeType.Element
                    If reader.Name = "default" Then
                        defaultBoolean = True
                    ElseIf reader.Name = "other" Then
                        otherBoolean = True
                    ElseIf reader.Name = "links" Then
                        linksBoolean = True
                    ElseIf reader.Name = "package" Then
                        If defaultBoolean Then
                            Dim stringData As String() = {reader.GetAttribute("id"), reader.GetAttribute("title"), reader.GetAttribute("filename")}
                            defaultQueue.Enqueue(stringData)

                        ElseIf otherBoolean Then
                            Dim stringData As String() = {reader.GetAttribute("id"), reader.GetAttribute("title"), reader.GetAttribute("filename")}
                            otherQueue.Enqueue(stringData)

                        ElseIf linksBoolean Then
                            Dim stringData As String() = {reader.GetAttribute("title"), reader.GetAttribute("link")}
                            linksQueue.Enqueue(stringData)
                        End If
                    End If
                Case XmlNodeType.EndElement
                    If reader.Name = "default" Then
                        defaultBoolean = False
                    ElseIf reader.Name = "other" Then
                        otherBoolean = False
                    ElseIf reader.Name = "links" Then
                        linksBoolean = False
                    End If
            End Select
        End While
    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged

        packageProgress.Value = e.ProgressPercentage
        statusText.Text = DirectCast(e.UserState, String)
    End Sub

    Private Sub cancelDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelProcess.Click
        Me.BackgroundWorker1.CancelAsync()
    End Sub
End Class

This snippet is throwing a NullReferenceException.

VB.NET:
Expand Collapse Copy
Dim stringData As String() = {reader.GetAttribute("id"), reader.GetAttribute("title"), reader.GetAttribute("filename")}
defaultQueue.Enqueue(stringData)

My suspicion is that I do not grasp how to pass arrays, queues, or any objects as contents of a queue element. Thanks for you help in advance!
 
You have defined a variable of Queue type, but you haven't assign it an object instance. "New" keyword creates an instance.
Short way is "dim x as new something", long way "dim x as something = new something", you can also later in code assign a new object to same variable "x = new something".
 
Back
Top