Object null reference exception
-
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
-
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
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
-
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
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
-
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
-
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
I don't see anywhere in your code where you set the
Tag
to anything...then you try to cast theTag
into anXmlNode
. That's your problem. -
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
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 VB6On 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 theTry
/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 SubI'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 thexDoc.Load
and ifxDoc.DocumentElement
doesn't exist, though if it doesn't it means that thexDoc.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 areTreeNodes
. You don't need toDirectCast
them. -
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
-
I don't see anywhere in your code where you set the
Tag
to anything...then you try to cast theTag
into anXmlNode
. That's your problem.yes what u r saying is exactly right
hi
-
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.
How exactly are you deselecting a node? In a
TreeView
, you can't click on an object to deselect it. And, if you setTreeView1.SelectionNode = Nothing
then,
After_Select
doesn't get called. Show me example code that will fireAfter_Select
on a deselect. -
How exactly are you deselecting a node? In a
TreeView
, you can't click on an object to deselect it. And, if you setTreeView1.SelectionNode = Nothing
then,
After_Select
doesn't get called. Show me example code that will fireAfter_Select
on a deselect.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.