0

I'm trying to load the XML response text into an HTML object in windows forms application using visual studio, but its throwing an error

object reference is not set to an instance of an object

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim XMLReq As MSXML2.XMLHTTP60 = New MSXML2.XMLHTTP60
    Dim HTMLDoc As mshtml.HTMLDocument = New mshtml.HTMLDocument
    Dim Table As MSHTML.IHTMLElement
    URL = "https://tt.wiki.com/search?"
    XMLReq.open("GET", URL, False)
    XMLReq.send()
    If XMLReq.status <> 200 Then
        MsgBox("Error" & vbNewLine & XMLReq.status & " - " & XMLReq.statusText)
        Exit Sub
    End If
    Dim wb As WebBrowser = New WebBrowser
    HTMLDoc = wb.Document.DomDocument
    HTMLDoc.body.innerHTML = XMLReq.responseText   //object reference error occuring here//
    Table = HTMLDoc.getElementById("search_results")
End sub

the error is occurring at line:

HTMLDoc.body.innerHTML = XMLReq.responseText
James Z
  • 11,838
  • 10
  • 25
  • 41
Sandeep
  • 19
  • 4
  • Switch on Option Explicit and see what happens. Then debug and see on which line you're getting the null object. – Andrew Mortimer Dec 31 '18 at 07:37
  • i am writing code in visual studio environment, and i have not opt for Option Explicit. – Sandeep Dec 31 '18 at 07:42
  • the error is occurring at line HTMLDoc.body.innerHTML = XMLReq.responseText – Sandeep Dec 31 '18 at 07:47
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Richardissimo Dec 31 '18 at 08:46
  • Tip: note that the description of the [tag:visual-studio] tag says *"Use this tag if you have a specific question about Visual Studio features and functionality. DO NOT use this tag on questions regarding code which merely happened to be written in Visual Studio."*. I will edit the question to remove it for you. – Richardissimo Dec 31 '18 at 08:50
  • Hi Richard,thank you for editing question. i have gone through the link which you have posted but I'm still confused here that i have initialized the HTMLDoc object properly and trying to use the same, it is working fine with "Excel VBA Editor" but not in visual studio windows application. – Sandeep Dec 31 '18 at 09:11

1 Answers1

1

finally after so many attempts,i found the answer for this.... need to assign some HTML code to HTMLDoc object before accessing its body.innerHTML method. below is the code....it works fine for me`

Dim XMLReq As MSXML2.XMLHTTP60 = New MSXML2.XMLHTTP60
Dim HTMLDoc As mshtml.HTMLDocument = New mshtml.HTMLDocument
Dim HTMLDoc1 As mshtml.IHTMLDocument = HTMLDoc
Dim Table As MSHTML.IHTMLElement
URL = "https://tt.wiki.com/search?"
XMLReq.open("GET", URL, False)
XMLReq.send()
If XMLReq.status <> 200 Then
    MsgBox("Error" & vbNewLine & XMLReq.status & " - " & XMLReq.statusText)
    Exit Sub
End If
 HTMLDoc1.write("<html><body>test</body></html>")
    HTMLDoc1.close()
    HTMLDoc = HTMLDoc1
    HTMLDoc.body.innerHTML = XMLReq.responseText
    Table = HTMLDoc.getElementById("search_results")`
Sandeep
  • 19
  • 4