none
Null Reference Exception was unhandled RRS feed

  • Question

  • Hello

    I am using visual basic 2010 express. project about autofill combobox on website in visual basic webbrowser

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            
            If ComboBox2.SelectedIndex > -1 Then
                Dim selObj As HtmlElement = WebBrowser1.Document.GetElementById("addPassengerForm:psdetail:0:psgnGender")
                selObj.SetAttribute("value", DirectCast(ComboBox2.SelectedItem, System.Collections.DictionaryEntry).Key)  'This line shows null reference exception was unhandled.
            End If


    KAILASH PRAJAPATI

    Friday, August 1, 2014 4:33 AM

Answers

  • Null Reference means an object does not exist in memory it is not there and your code reference an object in code that has a null value. You can use the VS debugger, set a breakpoint, and single-step until you hit the line that blows up. You can redo the debug single-step but stop before the line that blows up. Then you can take the mouse pointer and hover over the objects on the line in question, and VS debugger will show you what are objects and what object is a null value.

    You should understand what the link is explaining to you. It doesn't matter if it's a .NET language like VB.NET or Java, because the principles are the same.

    http://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    Your code tries to address an object, there is no "instance" of the object is in memory, it's a null value for the object in memory and then it's going to be BOOM, BOOM and BOOM and it blows up on a null reference.

    Friday, August 1, 2014 4:56 AM
  • Try accessing the Web site in Internet Explorer, then press <F12> and use the tool to check the id attribute of the element. Is it really “addPassengerForm:psdetail:0:psgnGender”? If it is different, then adjust GetElementById.

    Friday, August 1, 2014 7:10 AM

All replies

  • Null Reference means an object does not exist in memory it is not there and your code reference an object in code that has a null value. You can use the VS debugger, set a breakpoint, and single-step until you hit the line that blows up. You can redo the debug single-step but stop before the line that blows up. Then you can take the mouse pointer and hover over the objects on the line in question, and VS debugger will show you what are objects and what object is a null value.

    You should understand what the link is explaining to you. It doesn't matter if it's a .NET language like VB.NET or Java, because the principles are the same.

    http://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    Your code tries to address an object, there is no "instance" of the object is in memory, it's a null value for the object in memory and then it's going to be BOOM, BOOM and BOOM and it blows up on a null reference.

    Friday, August 1, 2014 4:56 AM
  • Try accessing the Web site in Internet Explorer, then press <F12> and use the tool to check the id attribute of the element. Is it really “addPassengerForm:psdetail:0:psgnGender”? If it is different, then adjust GetElementById.

    Friday, August 1, 2014 7:10 AM