Results 1 to 3 of 3

Thread: [2005] Listbox error

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2003
    Location
    Auckland
    Posts
    1,139

    [2005] Listbox error

    I have this code....
    Code:
        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"

    What's wrong??

  2. #2
    Frenzied Member
    Join Date
    Aug 2005
    Posts
    1,042

    Re: [2005] Listbox error

    Dim XDoc As New Xml.XmlDocument
    That declared the variable, but then you have to set a refernece to that variable
    Set XDoc = New Xml.XmlDocument

    I think that will make it work.

    good Luck

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Listbox error

    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:
    1. Dim XDoc As New Xml.XmlDocument
    vb Code:
    1. Dim XDoc As Xml.XmlDocument = New Xml.XmlDocument
    vb Code:
    1. Dim XDoc As Xml.XmlDocument
    2.  
    3. 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:
    1. 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.
    Attached Images Attached Images  
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width