Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
ListBox1.Items.Clear()
Dim Lct As String = My.Settings.Location
' start xml to listbox
Dim XDoc As New Xml.XmlDocument
Try
'LOAD FILE INTO OBJECT
XDoc.Load(Lct & "\newslist.xml")
'LOOP ALL THE CHILD NODES OF THE DOCUMENT'S DOCUMENTELEMENT PROPERTY
For Each XNode As Xml.XmlNode In XDoc.DocumentElement.ChildNodes
Dim vv As Integer = XNode.Item("id").InnerText
ListBox1.Items.Add(XNode.Item("title").InnerText & " (" & XNode.Item("id").InnerText & ")")
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
' end xml to listbox
End Sub
But i get the error
"Object reference not set to an instance of an object"
You don't use 'Set' in VB.NET, plus that code already IS creating an XmlDocument object. These three code snippets all do exactly the same thing:
vb Code:
Dim XDoc As New Xml.XmlDocument
vb Code:
Dim XDoc As Xml.XmlDocument = New Xml.XmlDocument
vb Code:
Dim XDoc As Xml.XmlDocument
XDoc = New Xml.XmlDocument
That is not the problem. If your code throws an exception you should always specify what line it is thrown on. If the XDoc variable was a null reference then this line would be the issue:
vb Code:
XDoc.Load(Lct & "\newslist.xml")
but I'm quite sure that that is not the issue. I'm willing to bet that the issue is that either XNode.Item("id") or XNode.Item("title") are returning a null reference so getting their InnerText property will throw a NullReferenceException.
NullReferenceExceptions are pretty much the easiest exceptions to find. You simply test each reference on the line until you find one that's a null reference. The IDE can help you do that. Select an expression, right-click on it then select Quick Watch. Notice how the IDE points you right to the line that caused the problem, which you should then be pointing out to us. Also note that the unhandled exception dialogue has various links on it you can click to get more information, including View Detail, which can often be very helpful. The IDE has numerous debugging tools integrated into it. That's what the "I" in IDE stands for. It's a very rare thing indeed that you should have to resort to just reading your code and hoping that something pops out at you.