Variable Scope

G

Guest

I have the following and am crashing at the specified line. I've declared Function Globals which should extend the scope of a variable to the end of the funtion. But it seems that my varaiables are ending at the end of the case statement. They shouldn't be. I'm really starting to wondering if I found a bug being that the variables have been declared Globally.

Public Function Scrape(ByVal URL As String, ByVal ReadandWrite As Boolean, ByVal FileType As String) As Object

Dim objWebRequest As System.Net.HttpWebRequest
Dim objWebResponse As System.Net.HttpWebResponse
Dim file As System.IO.StreamReader
Dim strFile As String

Select Case URL
Case "Text File"
file = New System.IO.StreamReader(URL)
Case "URL File"
objWebRequest = CType(System.Net.WebRequest.Create(URL), System.Net.HttpWebRequest)
objWebRequest.Method = "GET"
objWebResponse = CType(objWebRequest.GetResponse(), System.Net.HttpWebResponse)
file = New System.IO.StreamReader(objWebResponse.GetResponseStream)
End Select

'Program crashes right here.
strFile = file.ReadToEnd

If ReadandWrite Then
Scrape = strFile
'Program crashes here too if the above line was set in the Global as 'Dim strFile as String = file.ReadToEnd
file.Close()
Return Scrape
End If
 
S

Scott M.

How about giving us some information about the exception that was thrown?
Since you know where it crashes, put that code in a Try...Catch statement.
Catch the exception and get it's type if need be.


Bryan said:
I have the following and am crashing at the specified line. I've declared
Function Globals which should extend the scope of a variable to the end of
the funtion. But it seems that my varaiables are ending at the end of the
case statement. They shouldn't be. I'm really starting to wondering if I
found a bug being that the variables have been declared Globally.
Public Function Scrape(ByVal URL As String, ByVal ReadandWrite As
Boolean, ByVal FileType As String) As Object
Dim objWebRequest As System.Net.HttpWebRequest
Dim objWebResponse As System.Net.HttpWebResponse
Dim file As System.IO.StreamReader
Dim strFile As String

Select Case URL
Case "Text File"
file = New System.IO.StreamReader(URL)
Case "URL File"
objWebRequest = CType(System.Net.WebRequest.Create(URL), System.Net.HttpWebRequest)
objWebRequest.Method = "GET"
objWebResponse = CType(objWebRequest.GetResponse(), System.Net.HttpWebResponse)
file = New System.IO.StreamReader(objWebResponse.GetResponseStream)
End Select

'Program crashes right here.
strFile = file.ReadToEnd

If ReadandWrite Then
Scrape = strFile
'Program crashes here too if the above line was set in the Global as 'Dim
strFile as String = file.ReadToEnd
 
G

Guest

Here is the error
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object

Source Error:

Line 23: If ReadandWrite The
Line 24: Scrape = strFil
Line 25: file.Close() 'Error occurs her
Line 26: Return Scrap
Line 27: End I


Source File: c:\inetpub\wwwroot\CompetitorKiller\ScreenScraper.vb Line: 25

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.
CompetitorKiller.ScreenScraper.Scrape(String URL, Boolean ReadandWrite, String FileType) in c:\inetpub\wwwroot\CompetitorKiller\ScreenScraper.vb:2
CompetitorKiller.WebForm1.Button1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\CompetitorKiller\WebForm1.aspx.vb:3
System.Web.UI.WebControls.Button.OnClick(EventArgs e
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData
System.Web.UI.Page.ProcessRequestMain(
 
Y

yEaH rIgHt

Your code doesn't make sense.

Change this line
Select Case URL

to read this:
Select Case FileType


I tried your code this way and it worked perfectly. Your select case
never selected any statement. That's why you got an error. Your
filestream was never craeted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top