WebException Throws NullReferenceException

Spilled

Member
Joined
Nov 28, 2008
Messages
17
Location
USA
Programming Experience
Beginner
Hello all when I try to Handle my WebException I'm getting a NullReferenceException when I try to Display the Error. Here is my code:

VB.NET:
Expand Collapse Copy
            Try
                res = req.GetResponse()
                RaiseEvent UpdateResponse(threadID, res.StatusCode.ToString & " - " & res.StatusDescription)
                res.Close()
            Catch Err As WebException
                If Err.Status = WebExceptionStatus.ProtocolError Then
                    RaiseEvent UpdateResponse(threadID, "401 - Unauthorised")
                Else
                    RaiseEvent UpdateResponse(threadID, CType(Err.Response, HttpWebResponse).StatusCode.ToString & " - " & CType(Err.Response, HttpWebResponse).StatusDescription)
                    RetryComboNeedNewProxy = True
                End If
            End Try
RaiseEvent UpdateResponse(threadID, CType(Err.Response, HttpWebResponse).StatusCode.ToString & " - " & CType(Err.Response, HttpWebResponse).StatusDescription)
Is where i get the NullReferenceException, Err.response is "nothing" in the debugger, WebException.Response suppose to return the WebResponse type of the exception? Thanks in advance.
 
The documentation indicates that if the Status IS ProtocolError then Response will contain a WebResponse. You're trying to get Response when Status is NOT ProtocolError, in which case it's obviously not guaranteed to not be Nothing.
 
Um, obviously in your case it's Nothing. Have you checked what the actual status is? Maybe that will give you a clue what the issue is. After all, for some errors it should be obvious that there'd be no response because the server couldn't have responded.
 
Back
Top