Object reference not set to an instance of an object

monfu

Member
Joined
Jul 2, 2004
Messages
8
Location
Malta
Programming Experience
3-5
Dear All

I have the following code, which when executed, is returning the following error:-
Object reference not set to an instance of an object

'A form that allows the user to open an XML file and edit its contents on a Datagrid.
'Then the user can save his changes back to the file

Imports System.Data
Imports System.Xml

Public Class GuidedPracticeExe2_1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents dgXML As System.Windows.Forms.DataGrid
Friend WithEvents btnLoadXml As System.Windows.Forms.Button
Friend WithEvents btnSaveXML As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.dgXML = New System.Windows.Forms.DataGrid
Me.btnLoadXml = New System.Windows.Forms.Button
Me.btnSaveXML = New System.Windows.Forms.Button
CType(Me.dgXML, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'dgXML
'
Me.dgXML.DataMember = ""
Me.dgXML.Dock = System.Windows.Forms.DockStyle.Bottom
Me.dgXML.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.dgXML.Location = New System.Drawing.Point(0, 78)
Me.dgXML.Name = "dgXML"
Me.dgXML.Size = New System.Drawing.Size(720, 384)
Me.dgXML.TabIndex = 9
'
'btnLoadXml
'
Me.btnLoadXml.Location = New System.Drawing.Point(264, 24)
Me.btnLoadXml.Name = "btnLoadXml"
Me.btnLoadXml.TabIndex = 8
Me.btnLoadXml.Text = "Load XML"
'
'btnSaveXML
'
Me.btnSaveXML.Location = New System.Drawing.Point(368, 24)
Me.btnSaveXML.Name = "btnSaveXML"
Me.btnSaveXML.TabIndex = 10
Me.btnSaveXML.Text = "Save XML"
'
'GuidedPracticeExe2_1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(720, 462)
Me.Controls.Add(Me.btnSaveXML)
Me.Controls.Add(Me.dgXML)
Me.Controls.Add(Me.btnLoadXml)
Me.Name = "GuidedPracticeExe2_1"
Me.Text = "GuidedPracticeExe2_1"
CType(Me.dgXML, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub

#End Region

Dim xdd As XmlDataDocument
Dim ds As DataSet

Private Sub btnLoadXml_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadXml.Click
xdd = New XmlDataDocument
'Create a new XMLTextReader on the file
Dim xtr As XmlTextReader = New XmlTextReader("..\Books.xml")
'Initialize the dataset by reading the schema from the XML doc
ds.ReadXmlSchema(xtr)
'Reset the XMLTextReader
xtr.Close()
xtr = New XmlTextReader("..\Books.xml")
'Tell it to ignore whitespace
xtr.WhitespaceHandling = WhitespaceHandling.None
'LOad the synchronized object
xdd.Load(xtr)
'Display the resultant dataset
dgXML.DataSource = ds
dgXML.DataMember = "Book"
'Clean Up
xtr.Close()
End Sub

Private Sub GuidedPracticeExe2_1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Retreive the associated dataset and store it
ds = xdd.DataSet()
End Sub

Private Sub btnSaveXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveXML.Click
'Create a new XmlTextWriter on the file
Dim xtw As XmlTextWriter = New XmlTextWriter("..\Books.xml", _
System.Text.Encoding.UTF8)
'write the doc to it
xdd.WriteTo(xtw)
xtw.Close()
End Sub
End Class


Can anybody help me please?

Thanks for your help and time

Johann
 
based on what i can tell you dont have the ds declared as a new dataset (hence the reference not set to an instance of the object) but it's kinda hard to read through it all cause you didnt put [ code ] [ /code ] around it all.

what you want is:
VB.NET:
 private ds as new dataset
 instead of
 dim ds as dataset

and this is just a suggestion but when i deal with dataset's and xml, i dont use the xml reader/writer i just let the dataset do the actual reading/writing then just bind the datagrid to the dataset.

here's how i read/write the xml directly from the dataset:
VB.NET:
 'Read:
 Dim dsSettings As New DataSet("AppSettings")  'AppSettings is the name of the table in the dataset
 dsSettings.ReadXml(AppSettings.xml)
 
 'Write:
 dsSettings.WriteXml(AppSettings.xml)
 

Latest posts

Back
Top