Advertisement
Jaflem92

XML vb.net Test Error NullReferenceException 7/5/2012

Jul 5th, 2012
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Imports System.IO
  2. Imports System.Xml
  3.  
  4. Public Class frmMain
  5.  
  6.     Dim dsPerson As New DataSet("Person")
  7.  
  8.     Private Sub Setup()
  9.         Dim People As DataTable = dsPerson.Tables.Add("People")
  10.  
  11.         Dim Name As DataColumn = People.Columns.Add("Name", Type.GetType("System.String"))
  12.         People.Columns.Add("Email", Type.GetType("System.String"))
  13.         People.Columns.Add("Telephone", Type.GetType("System.String"))
  14.         People.Columns.Add("Notes", Type.GetType("System.String"))
  15.  
  16.         People.PrimaryKey = New DataColumn() {Name}
  17.  
  18.         dsPerson.AcceptChanges()
  19.  
  20.         DataGridView1.DataSource = dsPerson
  21.         DataGridView1.DataMember = "People"
  22.         DataGridView1.Refresh()
  23.  
  24.     End Sub
  25.  
  26.  
  27.     Private Sub XMLRead()
  28.  
  29.         If (cbxXML.Text = "") Then
  30.  
  31.             MessageBox.Show("No file name entered")
  32.  
  33.         Else
  34.  
  35.             If (System.IO.File.Exists(cbxXML.Text.ToString())) Then
  36.  
  37.                 Dim document As XmlReader = New XmlTextReader(cbxXML.Text.ToString())
  38.                 Dim PersonRow As DataRow = dsPerson.Tables("People").NewRow()
  39.  
  40.                 While (document.Read())
  41.  
  42.                     Dim type = document.NodeType
  43.  
  44.                     If (type = XmlNodeType.Element) Then
  45.  
  46.                         Debug.Print(document.Name)
  47.  
  48.                         If (document.Name = "Person") Then
  49.  
  50.                             PersonRow = dsPerson.Tables("People").NewRow()
  51.  
  52.                         End If
  53.  
  54.                         If (document.Name = "Name") Then
  55.                             Dim x As String = document.ReadInnerXml.ToString()
  56.                             Debug.Print(x)
  57.                             PersonRow("Name") = x
  58.                         End If
  59.  
  60.                         If (document.Name = "Email") Then
  61.                             Dim x As String = document.ReadInnerXml.ToString()
  62.                             Debug.Print(x)
  63.                             PersonRow("Email") = x
  64.                         End If
  65.  
  66.                         If (document.Name = "Tel") Then
  67.                             Dim x As String = document.ReadInnerXml.ToString()
  68.                             Debug.Print(x)
  69.                             PersonRow("Telephone") = x
  70.                         End If
  71.  
  72.                         If (document.Name = "Notes") Then
  73.                             Dim x As String = document.ReadInnerXml.ToString()
  74.                             Debug.Print(x)
  75.                             PersonRow("Notes") = x
  76.                         End If
  77.  
  78.                     End If ' type = XmlNodeType.Element
  79.  
  80.                     If (type = XmlNodeType.EndElement) Then
  81.  
  82.                         Debug.Print("/" & document.Name)
  83.  
  84.                         If (document.Name = "Person") Then
  85.                             Debug.Print("*Attempt new row")
  86.                            
  87.                             dsPerson.Tables.Item("Person").Rows.Add(PersonRow) 'Error (Object reference
  88.                     'not set to an instance of an object. , NullReferenceException)
  89.                            
  90.                             Debug.Print("*New Row Failed")
  91.                            
  92.                         End If
  93.                     End If
  94.  
  95.                 End While 'document.read()
  96.  
  97.                 dsPerson.AcceptChanges()
  98.  
  99.             Else
  100.  
  101.                 MessageBox.Show("The filename you selected was not found.")
  102.  
  103.             End If
  104.  
  105.         End If
  106.  
  107.     End Sub
  108.  
  109.     Private Sub btnLoadXml_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadXml.Click
  110.  
  111.         XMLRead()
  112.  
  113.         Me.DataGridView1.DataSource = dsPerson.Tables("People")
  114.  
  115.     End Sub
  116.  
  117.     Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  118.         Setup()
  119.     End Sub
  120. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement