Occasional "Object reference not set to an instance of an object."-Error

Topics: User Forum
Mar 4, 2015 at 7:07 AM
Edited Mar 4, 2015 at 7:19 AM
First, thanks for the great FlickrNet work. I found this Library very useful!

I am having a problem while querying Flickr I couldn't solve in the last month. No matter what I do, I am getting a "Object reference not set to an instance of an object" uncaught Exception in about 0.01% of the queries.

This is the actual error, although it doesn't say much:
************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at GetGeo.GetGeoForm.queryFlickr(Flickr f, PhotoSearchOptions options, Int32 run, DateTime& maxdate, DateTime mindate, String AccessTFormat, Int32 MaxPhotos, Int32& total, Int32& pages, Int32& total_overall, Int32& total_lastquery, Int32& total_thisquery, Int32& total_off, List`1& allPhotos_sich, Boolean& ExitQuery, Int32 perpage, Int32& runall, Boolean& subgrid) in ...\Flickr\GETGEO\GetGeo.vb:line 881
at GetGeo.GetGeoForm.Button4_Click_1(Object sender, EventArgs e) in ...\GETGEO\GetGeo.vb:line 456
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
I have the feeling it is because of an interrupted Internet connection, and, in connection therewith, an empty response from f.PhotosSearch(options).
This is the respective part of code:
 Try
            Application.DoEvents()
            Photos = f.PhotosSearch(options) 'Get Flickr data based on options, I think this is where the NullReference Exception occurs, i.e. f.PhotosSearch is empty and its trying to write it to Photos
            Application.DoEvents()

            'Check for Empty results (this obviously doesn't work!)
            If IsNothing(Photos) And xi < 3 And ExitQuery = False Then
                xi = xi + 1
                Outputtextbox.Text &= "Empty response.. repeating query " & 3 - xi & " more times.." & vbCrLf
                Outputtextbox.SelectionStart = Outputtextbox.Text.Length
                Outputtextbox.ScrollToCaret()
                Outputtextbox.Invalidate()
                Me.Refresh()
                System.Threading.Thread.Sleep(2000)
                GoTo repeat
            ElseIf xi = 3 Then
                ExitQuery = True
                Outputtextbox.Text &= "Three times empty response: exiting." & vbCrLf
                Outputtextbox.SelectionStart = Outputtextbox.Text.Length
                Outputtextbox.ScrollToCaret()
                Outputtextbox.Invalidate()
                Me.Refresh()
                Exit Sub
            End If

'Other Catch Exceptions (this seems to work)
        Catch exWeb As System.Net.WebException 'Catch Errors such as 502 Bad gateway
            Dim exWebResponse As HttpWebResponse = exWeb.Response
            If exWebResponse.StatusCode = HttpStatusCode.BadGateway Then
                Outputtextbox.Text &= "WebException occured: 502 - No response from Server. Repeating query in 2 Seconds.." & vbCrLf
                System.Threading.Thread.Sleep(2000)
                GoTo repeat
            ElseIf exWebResponse.StatusCode = HttpStatusCode.GatewayTimeout Then
                Outputtextbox.Text &= "WebException occured: 504 - Gateway Timeout. Repeating query in 2 Seconds.." & vbCrLf
                System.Threading.Thread.Sleep(2000)
                GoTo repeat
            ElseIf exWebResponse.StatusCode = HttpStatusCode.RequestTimeout Then
                Outputtextbox.Text &= "WebException occured: Request Timeout. Repeating query in 2 Seconds.." & vbCrLf
                System.Threading.Thread.Sleep(2000)
                GoTo repeat
            ElseIf exWebResponse.StatusCode.ToString = "105" Then
                Outputtextbox.Text &= "WebException occured: 105 - Service currently unavailable (Site Disabled). Waiting 15 Minutes.." & vbCrLf
                System.Threading.Thread.Sleep(900000)
                GoTo repeat
            Else
                Outputtextbox.Text &= "General WebException occured: " & exWebResponse.StatusCode.ToString & " - " & exWeb.Message.ToString & "Repeating query in 2 Seconds.." & vbCrLf
                System.Threading.Thread.Sleep(2000)
                GoTo repeat
            End If
        Catch ex As System.Exception
            Outputtextbox.Text &= "Error occured: " & ex.Message
            Exit Sub
        End Try
How can I check whether f.PhotosSearch(options) is empty before assigning it to "Photos ="?
If I do
if not isnothing(f.PhotosSearch(options)) then Photos = f.PhotosSearch(options)
Flickr will be queried twice, right?

Thanks for any help!
Coordinator
Mar 4, 2015 at 1:38 PM
If f.PhotosSearch(options) return Nothing then Photos would be set to Nothing. This would not throw a NullReferenceException.

I don't believe PhotosSearch ever returns Nothing however - it might return a collection of photos with no elements.

Are you using the latest version from NuGet?

Can you identify which line is 881? Is there any chance that "options" is actually null?

Sam
Mar 4, 2015 at 2:20 PM
Edited Mar 4, 2015 at 2:26 PM
Hi Sam,

Line 880: Catch exWeb As System.Net.WebException 'Catch Errors such as 502 Bad gateway https://flickrnet.codeplex.com/discussions/549197
Line 881: Dim exWebResponse As HttpWebResponse = exWeb.Response

I don't understand it, though.. if there is an exWeb, isn't there always an exWeb.Response?

I am using the FlickrNet 3.13 Release with NuGet. I don't think this problem has to do something with options not set.
I was using my program for over a year now without any problems, but these days I am working from a very bad Internet connection which seems not stable. This was why I suspected an empty response (if, in the middle of an API Call, the connection just hangs up). Also, I get the same error if I just disable WLAN while my program is running.

Thanks!

[edit]
I will try if this works:
  If Not IsNothing(exWeb.Response) Then
                Dim exWebResponse As HttpWebResponse = exWeb.Response
...
  End If
Coordinator
Mar 4, 2015 at 7:56 PM
No, WebException.Response is often null. It would only be not null if the exception is sent from the actual server. Most network errors would have no response.

You're suggested code should work.

Sam
Marked as answer by Gromhelm on 3/5/2015 at 5:56 AM
Mar 5, 2015 at 1:46 PM
Hi Sam,

this totally makes sense. It also explains, why I WebException.Response is NULL when I disconnect my Internet Connection!
I know this has nothing to do with FlickrNet, so double thanks for your hint!

Best,
Helmut