How can I avoid the dreaded null reference exception?

By DavidCon999 ·
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
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?

This conversation is currently closed to new comments.

2 total posts (Page 1 of 1)  
Thread display: Collapse - | Expand +

All Answers

Collapse -

Well given the previous line and the assignment

by Tony Hopkinson In reply to How can I avoid the dread ...

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

Collapse -

Reponse To Answer

by DavidCon999 In reply to Well given the previous l ...

Thanks for responding.

blank.html does have valid <body> and </body> 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.

Back to Windows Forum
2 total posts (Page 1 of 1)  

Related Discussions

Related Forums