Question Locked

How can I avoid the dreaded null reference exception?

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I know that the null reference exception is thrown when a reference is made to something that is null or nothing. I'm getting the error but I can't see what it is that is null.

My program (writen in VB.NET 2010) is one that allows non HTML folk to edit web pages and it works fine in the main Windows form I've created. We have a footer that uses a server side include file and I set up a dialog box to allow users to edit the contents of the footer in a WebBrowser control.

My dialog box load event is coded as follows:

Private Sub FooterDialog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strInput As String
strInput = My.Computer.FileSystem.ReadAllText(LocalWebSiteFolder & "footer.html")
With Me.WebBrowser1
.Navigate(LocalWebSiteFolder & "blank.html")
.Document.ExecCommand("EditMode", False, Nothing)
.Document.Body.InnerHtml = strInput
.Focus()
End With
End Sub

The null reference exception occurs on the line where I try to set the InnerHTML of the WebBrowser document to my strInput variable. Before you ask, the reason I navigate to blank.html before inserting the HTML text from footer.html is simply the footer contains no formatting and the Webbrowser looks awful without it. Anyway, I use the same technique on the main form and it works fine.

Any ideas where I'm going wrong?

3 replies

by tony hopkinson

that suggests that Body is null, so does blank.html have a body tag...

by davidcon999

Thanks for responding.

blank.html does have valid and tags.

I've been experimenting and now have it working by removing the loading of blank.html and setting editmode to an initialise interface routine that is run before the form loads. All that happens when the form loads is setting the InnerHTML = strInput.

I don't yet understand why this works but it does.

This discussion is closed to new replies.