0

I get this error when i run my program:

A first chance exception of type 'System.NullReferenceException' occurred in Microsoft.VisualBasic.dll

Object variable or with block variable not set

Here is my code:

    Dim rt As String = ""
    Dim out As String
    Dim wRequest As WebRequest
    Dim wResponse As WebResponse
    Dim SR As StreamReader
    Dim time As Date

    time = Now()

    Try
        wRequest = WebRequest.Create(Address)
        wRequest.Timeout = 10000
        wResponse = wRequest.GetResponse
        SR = New StreamReader(wResponse.GetResponseStream)
        rt = SR.ReadToEnd
        SR.Close()
    Catch wex As WebException

        Dim status As WebExceptionStatus = wex.Status

        If status = WebExceptionStatus.Timeout Then
            MessageBox.Show("Could not establish a connection to the selected exchange server.", "Connection Timed Out", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        ElseIf status = WebExceptionStatus.ConnectFailure Then
            MessageBox.Show("Could not establish a connection to the selected exchange server.", "Connection Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        ElseIf status = WebExceptionStatus.ProtocolError Then
            MessageBox.Show("Could not establish a connection to the selected exchange server.", "Connection Protocol Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)

        End If

    End Try
Community
  • 1
  • 1
Skilo Skilo
  • 526
  • 4
  • 11
  • 23

3 Answers3

0

The source of your error could be your Address variable. Please try prefix with http:// in front.

Example:

Address = "http://www.google.com"

for more further information, please read MSDN WebRequest.Create Method (String)

Dennis
  • 3,528
  • 4
  • 28
  • 40
0

I am checking your code and it works fine.

Here's a demo although I made a little change on the declaration of time variable and putting a string on WebRequest.Create() like:

Dim time As Date = Now

and

WebRequest.Create("https://www.google.fm")

And as per my own search there is nothing much to be concerned about with this kind of error, see the link below.

A first chance exception

Community
  • 1
  • 1
Edper
  • 9,144
  • 1
  • 27
  • 46
0

The problem is most likely that wResponse.GetResponseStream is failing because wResponse is null. (which is probably because your Address variable isn't valid).

Try adding

Catch ex As Exception

    MessageBox.Show("Some other error occurred: " + ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Warning)

End Try

after your WebException Catch block to see what the problem is.

Or just put a breakpoint on SR = New StreamReader(wResponse.GetResponseStream) and look at wResponse (your choice).

DeanOC
  • 7,142
  • 6
  • 42
  • 56
  • The way i set up the function i pass the address variable to the function with a button like this 'textbox1.text = getData("http://somesite.com")' – Skilo Skilo Jul 04 '13 at 02:53
  • I'm not sure how that relates to your `null ref` exception. If the site can be entered by the user, you should always code for the case where an invalid address is entered and handle it. – DeanOC Jul 04 '13 at 03:35