Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Object null reference exception

Object null reference exception

Scheduled Pinned Locked Moved Visual Basic
xmlhelplounge
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ademsandeepreddy
    wrote on last edited by
    #1

    write know i am gettin null reference exception due to followin code Imports System.Xml Imports System.IO Public Class TreeView Dim xNode As XmlNode Private Sub btnOpenXml_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenXml.Click populateTreeview() End Sub Private Function SelectTextFile() As String Dim fDialog As New OpenFileDialog() fDialog.Filter = "xml files (*.xml)|*.xml" fDialog.InitialDirectory = "~\Desktop" Return If((fDialog.ShowDialog() = DialogResult.OK), fDialog.FileName, Nothing) End Function Private Sub populateTreeview() Try 'Just a good practice -- change the cursor to a 'wait cursor while the nodes populate Me.Cursor = Cursors.WaitCursor 'First, we'll load the Xml document Dim xDoc As New XmlDocument() xDoc.Load(SelectTextFile()) 'Now, clear out the treeview, 'and add the first (root) node tvXmlTree.Nodes.Clear() tvXmlTree.Nodes.Add(New TreeNode(xDoc.DocumentElement.Name)) Dim tNode As New TreeNode() tNode = DirectCast(tvXmlTree.Nodes(0), TreeNode) 'We make a call to addTreeNode, 'where we'll add all of our nodes addTreeNode(xDoc.DocumentElement, tNode) 'Expand the treeview to show all nodes tvXmlTree.CollapseAll() Catch xExc As XmlException 'Exception is thrown is there is an error in the Xml MessageBox.Show(xExc.Message) Catch ex As Exception 'General exception MessageBox.Show(ex.Message) Finally 'Change the cursor back Me.Cursor = Cursors.[Default] End Try End Sub Private Sub addTreeNode(ByVal xmlNode As XmlNode, ByVal treeNode As TreeNode) Dim tNode As TreeNode Dim xNodeList As XmlNodeList If xmlNode.HasChildNodes Then 'The current node has children xNodeList = xmlNode.ChildNodes For x As Integer = 0 To xNodeList.Count - 1 'Loop through the child nodes xNode = xmlNode.ChildNodes(x) treeNode.Nodes.Add(New TreeNode(xNode.Name)) tNode = treeNode.Nodes(x) addTreeNode(xNode, tNode) Next Else 'No children, so add the outer xml (trimming off whitespace) 'treeNode.Text = xmlNode.OuterXml.Trim() ' treeNode.Text = xmlNode.LocalName.Trim() End If End Sub Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click tvXmlTree.Refresh() End Sub Private Sub tvXmlTree_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvXmlTree.AfterSelect 'xNode = DirectCast(tvXmlTree.SelectedNode.Tag, XmlNode) 'Me.txtContent.Text = xNode.InnerXml End Sub Private Sub btnExit_Click(ByVal sender As

    D W 2 Replies Last reply
    0
    • A ademsandeepreddy

      write know i am gettin null reference exception due to followin code Imports System.Xml Imports System.IO Public Class TreeView Dim xNode As XmlNode Private Sub btnOpenXml_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenXml.Click populateTreeview() End Sub Private Function SelectTextFile() As String Dim fDialog As New OpenFileDialog() fDialog.Filter = "xml files (*.xml)|*.xml" fDialog.InitialDirectory = "~\Desktop" Return If((fDialog.ShowDialog() = DialogResult.OK), fDialog.FileName, Nothing) End Function Private Sub populateTreeview() Try 'Just a good practice -- change the cursor to a 'wait cursor while the nodes populate Me.Cursor = Cursors.WaitCursor 'First, we'll load the Xml document Dim xDoc As New XmlDocument() xDoc.Load(SelectTextFile()) 'Now, clear out the treeview, 'and add the first (root) node tvXmlTree.Nodes.Clear() tvXmlTree.Nodes.Add(New TreeNode(xDoc.DocumentElement.Name)) Dim tNode As New TreeNode() tNode = DirectCast(tvXmlTree.Nodes(0), TreeNode) 'We make a call to addTreeNode, 'where we'll add all of our nodes addTreeNode(xDoc.DocumentElement, tNode) 'Expand the treeview to show all nodes tvXmlTree.CollapseAll() Catch xExc As XmlException 'Exception is thrown is there is an error in the Xml MessageBox.Show(xExc.Message) Catch ex As Exception 'General exception MessageBox.Show(ex.Message) Finally 'Change the cursor back Me.Cursor = Cursors.[Default] End Try End Sub Private Sub addTreeNode(ByVal xmlNode As XmlNode, ByVal treeNode As TreeNode) Dim tNode As TreeNode Dim xNodeList As XmlNodeList If xmlNode.HasChildNodes Then 'The current node has children xNodeList = xmlNode.ChildNodes For x As Integer = 0 To xNodeList.Count - 1 'Loop through the child nodes xNode = xmlNode.ChildNodes(x) treeNode.Nodes.Add(New TreeNode(xNode.Name)) tNode = treeNode.Nodes(x) addTreeNode(xNode, tNode) Next Else 'No children, so add the outer xml (trimming off whitespace) 'treeNode.Text = xmlNode.OuterXml.Trim() ' treeNode.Text = xmlNode.LocalName.Trim() End If End Sub Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click tvXmlTree.Refresh() End Sub Private Sub tvXmlTree_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvXmlTree.AfterSelect 'xNode = DirectCast(tvXmlTree.SelectedNode.Tag, XmlNode) 'Me.txtContent.Text = xNode.InnerXml End Sub Private Sub btnExit_Click(ByVal sender As

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      1. Format the code using "pre" tags. 2. Don't post entire code. Just post the relevant parts. 3. Something is null in your code while you try to use it. Debug and check.

      "No matter how many fish in the sea; it will be so empty without me." - From song "Without me" by Eminem

      A 1 Reply Last reply
      0
      • D dan sh

        1. Format the code using "pre" tags. 2. Don't post entire code. Just post the relevant parts. 3. Something is null in your code while you try to use it. Debug and check.

        "No matter how many fish in the sea; it will be so empty without me." - From song "Without me" by Eminem

        A Offline
        A Offline
        ademsandeepreddy
        wrote on last edited by
        #3

        sorry here is the code Private Sub tvXmlTree_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvXmlTree.AfterSelect xNode = DirectCast(tvXmlTree.SelectedNode.Tag, XmlNode) Me.txtContent.Text = xNode.InnerXml End Sub here i am using treeview control i had to get the xml from node node selected i am getting null reference exception at the innerxml Thanks and regards SANDEEP ADEM

        hi

        D W R 3 Replies Last reply
        0
        • A ademsandeepreddy

          sorry here is the code Private Sub tvXmlTree_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvXmlTree.AfterSelect xNode = DirectCast(tvXmlTree.SelectedNode.Tag, XmlNode) Me.txtContent.Text = xNode.InnerXml End Sub here i am using treeview control i had to get the xml from node node selected i am getting null reference exception at the innerxml Thanks and regards SANDEEP ADEM

          hi

          D Offline
          D Offline
          dan sh
          wrote on last edited by
          #4

          If that cast is working properly and XmlNode is getting created properly.

          "No matter how many fish in the sea; it will be so empty without me." - From song "Without me" by Eminem

          1 Reply Last reply
          0
          • A ademsandeepreddy

            sorry here is the code Private Sub tvXmlTree_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvXmlTree.AfterSelect xNode = DirectCast(tvXmlTree.SelectedNode.Tag, XmlNode) Me.txtContent.Text = xNode.InnerXml End Sub here i am using treeview control i had to get the xml from node node selected i am getting null reference exception at the innerxml Thanks and regards SANDEEP ADEM

            hi

            W Offline
            W Offline
            William Winner
            wrote on last edited by
            #5

            I don't see anywhere in your code where you set the Tag to anything...then you try to cast the Tag into an XmlNode. That's your problem.

            A 1 Reply Last reply
            0
            • A ademsandeepreddy

              write know i am gettin null reference exception due to followin code Imports System.Xml Imports System.IO Public Class TreeView Dim xNode As XmlNode Private Sub btnOpenXml_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenXml.Click populateTreeview() End Sub Private Function SelectTextFile() As String Dim fDialog As New OpenFileDialog() fDialog.Filter = "xml files (*.xml)|*.xml" fDialog.InitialDirectory = "~\Desktop" Return If((fDialog.ShowDialog() = DialogResult.OK), fDialog.FileName, Nothing) End Function Private Sub populateTreeview() Try 'Just a good practice -- change the cursor to a 'wait cursor while the nodes populate Me.Cursor = Cursors.WaitCursor 'First, we'll load the Xml document Dim xDoc As New XmlDocument() xDoc.Load(SelectTextFile()) 'Now, clear out the treeview, 'and add the first (root) node tvXmlTree.Nodes.Clear() tvXmlTree.Nodes.Add(New TreeNode(xDoc.DocumentElement.Name)) Dim tNode As New TreeNode() tNode = DirectCast(tvXmlTree.Nodes(0), TreeNode) 'We make a call to addTreeNode, 'where we'll add all of our nodes addTreeNode(xDoc.DocumentElement, tNode) 'Expand the treeview to show all nodes tvXmlTree.CollapseAll() Catch xExc As XmlException 'Exception is thrown is there is an error in the Xml MessageBox.Show(xExc.Message) Catch ex As Exception 'General exception MessageBox.Show(ex.Message) Finally 'Change the cursor back Me.Cursor = Cursors.[Default] End Try End Sub Private Sub addTreeNode(ByVal xmlNode As XmlNode, ByVal treeNode As TreeNode) Dim tNode As TreeNode Dim xNodeList As XmlNodeList If xmlNode.HasChildNodes Then 'The current node has children xNodeList = xmlNode.ChildNodes For x As Integer = 0 To xNodeList.Count - 1 'Loop through the child nodes xNode = xmlNode.ChildNodes(x) treeNode.Nodes.Add(New TreeNode(xNode.Name)) tNode = treeNode.Nodes(x) addTreeNode(xNode, tNode) Next Else 'No children, so add the outer xml (trimming off whitespace) 'treeNode.Text = xmlNode.OuterXml.Trim() ' treeNode.Text = xmlNode.LocalName.Trim() End If End Sub Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click tvXmlTree.Refresh() End Sub Private Sub tvXmlTree_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvXmlTree.AfterSelect 'xNode = DirectCast(tvXmlTree.SelectedNode.Tag, XmlNode) 'Me.txtContent.Text = xNode.InnerXml End Sub Private Sub btnExit_Click(ByVal sender As

              W Offline
              W Offline
              William Winner
              wrote on last edited by
              #6

              I gotta say, this is badly written. First, why would you put an entire sub within a Try/Catch block? If you want the old VB6 On Error, you can still use it...though it's not suggested. Only put the sections of code that you think could throw an exception within the Try/Catch. That's what it's there for.

              Private Sub populateTreeview()
              Try
              * 'Just a good practice -- change the cursor to a
              * 'wait cursor while the nodes populate
              * Me.Cursor = Cursors.WaitCursor
              * 'First, we'll load the Xml document
              * Dim xDoc As New XmlDocument()
              xDoc.Load(SelectTextFile())
              * 'Now, clear out the treeview,
              * 'and add the first (root) node
              * tvXmlTree.Nodes.Clear()
              tvXmlTree.Nodes.Add(New TreeNode(xDoc.DocumentElement.Name))
              * Dim tNode As New TreeNode()
              * tNode = DirectCast(tvXmlTree.Nodes(0), TreeNode)
              * 'We make a call to addTreeNode,
              * 'where we'll add all of our nodes
              * addTreeNode(xDoc.DocumentElement, tNode)
              * 'Expand the treeview to show all nodes
              * tvXmlTree.CollapseAll()
              Catch xExc As XmlException
              'Exception is thrown is there is an error in the Xml
              MessageBox.Show(xExc.Message)
              Catch ex As Exception
              'General exception
              MessageBox.Show(ex.Message)
              Finally
              'Change the cursor back
              Me.Cursor = Cursors.[Default]
              End Try
              End Sub

              I've put asterisks besides the lines of code that should never throw an exception. So, there are only two lines that could possible throw an error...if the SelectFile() doesn't return a valid item for the xDoc.Load and if xDoc.DocumentElement doesn't exist, though if it doesn't it means that the xDoc.Load didn't work which should have already thrown an exception. And, by the way,

              DirectCast(tvXmlTree.Nodes(0), TreeNode)

              in this line, all TreeView Nodes are TreeNodes. You don't need to DirectCast them.

              1 Reply Last reply
              0
              • A ademsandeepreddy

                sorry here is the code Private Sub tvXmlTree_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvXmlTree.AfterSelect xNode = DirectCast(tvXmlTree.SelectedNode.Tag, XmlNode) Me.txtContent.Text = xNode.InnerXml End Sub here i am using treeview control i had to get the xml from node node selected i am getting null reference exception at the innerxml Thanks and regards SANDEEP ADEM

                hi

                R Offline
                R Offline
                rhuiden
                wrote on last edited by
                #7

                You should also check tvXmlTree.SelectedNode to make sure something is selected. If you deselect a node then this event will fire and your code will fail.

                W 1 Reply Last reply
                0
                • W William Winner

                  I don't see anywhere in your code where you set the Tag to anything...then you try to cast the Tag into an XmlNode. That's your problem.

                  A Offline
                  A Offline
                  ademsandeepreddy
                  wrote on last edited by
                  #8

                  yes what u r saying is exactly right

                  hi

                  1 Reply Last reply
                  0
                  • R rhuiden

                    You should also check tvXmlTree.SelectedNode to make sure something is selected. If you deselect a node then this event will fire and your code will fail.

                    W Offline
                    W Offline
                    William Winner
                    wrote on last edited by
                    #9

                    How exactly are you deselecting a node? In a TreeView, you can't click on an object to deselect it. And, if you set

                    TreeView1.SelectionNode = Nothing

                    then, After_Select doesn't get called. Show me example code that will fire After_Select on a deselect.

                    R 1 Reply Last reply
                    0
                    • W William Winner

                      How exactly are you deselecting a node? In a TreeView, you can't click on an object to deselect it. And, if you set

                      TreeView1.SelectionNode = Nothing

                      then, After_Select doesn't get called. Show me example code that will fire After_Select on a deselect.

                      R Offline
                      R Offline
                      rhuiden
                      wrote on last edited by
                      #10

                      Sorry, I didn't verify that. I thought that was the case but I suppose that is left over from working on an Access application :( . Which I know behaves that way. However, I do try to check all variables before using them, even when they are "guaranteed" to be instantiated.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups