Question "reference not set to an instance" when calling a sub

Ragman

New member
Joined
Jul 16, 2009
Messages
4
Programming Experience
10+
The first sub calls LoadRaceTimer without a problem. The second results in a "object reference not set to an instance of an object" error. I can't for the life of me figure out why. All three are in "Public Class MainScreen". Any help is appreciated.

VB.NET:
Private Sub SessionsBrowser_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles SessionsBrowser.DocumentCompleted
...
LoadRaceTimer()
...
End Sub

VB.NET:
Private Sub ButtonRefreshSessions_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonRefreshSessions.Click
...
LoadRaceTimer()
...
End Sub

VB.NET:
Public Sub LoadRaceTimer()
...
End Sub
 
What is in the LoadRaceTimer? Have you debugged it and use a breakpoint to check each line in the sub when you go thru it the second time?
 
What is in the LoadRaceTimer? Have you debugged it and use a breakpoint to check each line in the sub when you go thru it the second time?

Hey newguy. Thanks for taking the time.

I'm getting strange results. I had thought that since the Try/Catch that tripped was in the Click routine and not LoadRaceTimer, then that's were the error was. I had the line
VB.NET:
If SessionsBrowser.Document.GetElementById("sessions_div") IsNot Nothing Then
in LoadRaceTimer, which was causing a cast error, so I just went ahead with
VB.NET:
webpage = SessionsBrowser.Document.GetElementById("sessions_div").InnerHtml.ToString
without the check first and put it outside the Try/Catch, since it seemed to run ok from the Navigated sub. That's the line that the triggers the error in the Click routine (even though it's in LoadRaceTimer).

The weird part is, I put the "If IsNot Nothing" check back in and it doesn't trip an error now. However, the Click routine is just refreshing the browser page (Completely) and rereading info on it, so I don't know why it finds that ID whenever the page is changed, but not when refreshed. It should always be there. Oi.

Thanks for getting me pointed in the right direction. It would be so much easier if a WebBrowser.Refresh() triggered the non-existant OnRrefresh, or at least Navigating/Navigated/etc. As it is, I don't see any way to have a refresh trigger *anything*.
 
the Click routine is just refreshing the browser page (Completely) and rereading info on it, so I don't know why it finds that ID whenever the page is changed, but not when refreshed. It should always be there.

I had to move the time delay that allows the Javascript to play out into the LoadRaceTimer sub instead of having it in the Click routine. So strange. It comes after the refresh either way.

Things seem to be working for now, at least until the next bug.
 
One of the functions I like use is the IsNothing(object). Bugs are funny little creatures yes. Glad you got it working.
 
One of the functions I like use is the IsNothing(object). Bugs are funny little creatures yes. Glad you got it working.

Ooh, thanks! I probably should grab a book. I've been winging it so far, as the web and MSDN library is so chock full of info. Need to know what to look for, though.
 
Back
Top