Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Code Support: ASP.NET (Non-Forum Related)
 Object Reference is Not Set to an Instance of an O
 New Topic  Topic Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

blackinwhite
Average Member

Turkey
657 Posts

Posted - 25 January 2005 :  09:57:02  Show Profile
Dear all,

I am trying to write and better my skills coding some small junk of codes.

With the example codes from MSDN I have managed to write the below code in order to edit some selected node from an xml file.

But the below code gives me an error unexpectedly. :(

It says Object Reference is Not Set to an Instance of an Object at the line highlighted below.

I think root or id returns null.

can anyone help me about this.



Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
'Try
'Send updates to xml file
Dim xDoc As XmlDocument = New XmlDocument()
Dim root As XmlNode = xDoc.DocumentElement
Dim id As XmlNode
Dim i As Integer
'root = xDoc.DocumentElement

For i = 0 To root.ChildNodes.Count - 1
id = root.ChildNodes(i).Attributes("CustomerID")
If (id.InnerText = txtUniqueID.Text) Then
xDoc.DocumentElement("CustomerID").Value = txtUniqueID.Text
xDoc.DocumentElement("CompanyName").Value = txtCoName.Text
xDoc.DocumentElement("ContactName").Value = txtContact.Text
xDoc.DocumentElement("ContactTitle").Value = txtTitle.Text
xDoc.DocumentElement("Address1").Value = txtAddress.Text
xDoc.DocumentElement("Phone1").Value = txtPhone1.Text
xDoc.DocumentElement("Phone2").Value = txtPhone2.Text
xDoc.DocumentElement("reason").Value = txtservice.Text
End If
Next i
xDoc.Save(m_connCustomers)

'Reset and begin again
LoadDataSet()
MsgBox("Record has been updated.")
'Attempt to update the datasource.
Me.LoadDataSet()
'Catch eUpdate As System.Exception
' 'Add your error handling code here.
' 'Display error message, if any.
' System.Windows.Forms.MessageBox.Show(eUpdate.Message)
'End Try
Me.dsCust_PositionChanged()
End Sub

[/CODE]

Best,

DavidRhodes
Senior Member

United Kingdom
1222 Posts

Posted - 27 January 2005 :  16:56:45  Show Profile
That error usually means that it can't find anything to reference, are you sure there are child nodes? Your not actually opening an xml doc are you? You're creating a new one so there won't be any child nodes defined

The UK MkIVs Forum
Go to Top of Page

Podge
Support Moderator

Ireland
3775 Posts

Posted - 27 January 2005 :  17:18:24  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
Looks like you're trying to write a new xml file.

Try something like this;


    Public Sub writeSettings()
        Dim writer As XmlTextWriter
        writer = New XmlTextWriter("settings.xml", Nothing)
        writer.WriteStartDocument()
        writer.Formatting = Formatting.Indented
        writer.Indentation = 3
        writer.WriteStartElement("settings")
        writer.WriteElementString("timeout", settingTimeout)
        writer.WriteElementString("useProxy", useProxy)
        writer.WriteElementString("proxyAddress", proxyAddress)
        writer.WriteElementString("proxyPort", proxyPort)
        writer.WriteElementString("useProxyAuthentication", useProxyAuthentication)
        writer.WriteElementString("proxyUsername", proxyUsername)
        writer.WriteElementString("proxyPassword", proxyPassword)
        writer.WriteElementString("proxyDomain", proxyDomain)
        writer.WriteEndElement()
        'writer.WriteEndElement()
        writer.Flush()
        writer.Close()
        writer = Nothing
    End Sub


Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.
Go to Top of Page

Podge
Support Moderator

Ireland
3775 Posts

Posted - 27 January 2005 :  17:24:29  Show Profile  Send Podge an ICQ Message  Send Podge a Yahoo! Message
If you're trying to add a new node try something like this;


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'newForum Save settings
        Dim xmlDoc As New XmlDocument
        xmlDoc.Load("forums.xml")
        Dim documentNode As XmlNode
        Dim cloneDocumentElement As XmlNode
        documentNode = xmlDoc.SelectSingleNode("/settings/forum")
        cloneDocumentElement = documentNode.CloneNode(True)
        documentNode.ParentNode.AppendChild(cloneDocumentElement)
        cloneDocumentElement.Item("monitorForum").InnerText = Me.monitorForum.Checked.ToString
        cloneDocumentElement.Item("url").InnerText = Me.ForumUrlTextBox.Text.ToString
        cloneDocumentElement.Item("username").InnerText = Me.username.Text.ToString
        cloneDocumentElement.Item("password").InnerText = Me.password.Text.ToString
        cloneDocumentElement.Item("notifyActiveTopics").InnerText = Me.notifyAT.Checked.ToString
        cloneDocumentElement.Item("notifyPrivateMessages").InnerText = Me.notifyPM.Checked.ToString
        cloneDocumentElement.Item("notifySubscribedTopics").InnerText = Me.notifyST.Checked.ToString
        cloneDocumentElement.Item("notifyEveryTimeout").InnerText = Me.notifyET.Checked.ToString
        xmlDoc.Save("forums.xml")
    End Sub

Podge.

The Hunger Site - Click to donate free food | My Blog | Snitz 3.4.05 AutoInstall (Beta!)

My Mods: CAPTCHA Mod | GateKeeper Mod
Tutorial: Enable subscriptions on your board

Warning: The post above or below may contain nuts.
Go to Top of Page

JamesCurran
Starting Member

USA
13 Posts

Posted - 01 February 2005 :  12:45:54  Show Profile  Visit JamesCurran's Homepage
quote:
Originally posted by blackinwhite
I think root or id returns null.


To be exact, both root and id are null.

You can't write to things that are there. You have to create them first.

        Dim xDoc As XmlDocument = New XmlDocument()
        Dim root As XmlNode = xDoc.DocumentElement

First you create a complete empty Xml document. Then you set root the the root element of xDoc. But Xdoc is completely empty -- it has no root element. SO, root is null.


        Dim xDoc As XmlDocument = New XmlDocument()
        Dim root As XmlNode = xDoc.CreateElement("MyRoot");
        xDoc.AppendChild(root);

Here we've create a root element, and added it to the document.


           For i = 0 To root.ChildNodes.Count - 1


Since (originally) root was null, root.ChildElement would be attempting to dereference a null value -- hence the error you got. With the changes I showed above, it would cause an error, but since we just create root, it doesn't have any children yet.

The next problem you have is that you refer to CustomerID as both an attribute & as an element. Which is it?
Attribute:
<someElement CustomerID="1234>

Element:
<CustomerID>1234</CustomerID>

Answering that determine how the rest would be written --- except I don't think you want that at all. It seem that you want to modify an existing XML file, inwhich case you'll need a

xDoc.Load(fileName)


before the

       Dim root As XmlNode = xDoc.DocumentElement





Truth,
James Curran
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000 Version 3.4.07