Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object not set to an instance of an object

Status
Not open for further replies.

Jackxxx

Programmer
Jun 21, 2007
31
US
Does anyone see why I would get this error? Object not set to an instance of an object

Here is my code and I get the error on the DirectCast line.
I have 5 labels on my pae Label1, label2, label3, label4, label5.

Dim responseStream As Stream = theResponse.GetResponseStream()
Dim streamReader As StreamReader = New StreamReader(responseStream)
Dim responseText As String = streamReader.ReadToEnd

Dim contents As String = responseText.ToString
If contents.Length = 0 Then Exit Function

Dim i As Integer = 0
Dim intCount As Integer = 0
Dim emails As String()
Dim email As String
Dim results As New StringBuilder
emails = ExtractEmailAddressesFromString(contents)
For i = 1 To 5
For Each email In emails
intCount += 1
results.Append(email & vbNewLine)
Next
DirectCast(FindControl(String.Format("Label{0}", CStr(i))), Label).Text = strURI.Substring(strURI.ToString().LastIndexOf("/") + 1) & ": " & intCount.ToString()
Next
 
I see what you are trying to do. but why would you do it this way? there is no real gain anywhere by accessing the data & objects this way.

you should not need to access the request or response stream directly these are the lowest level objects used by the asp.net framework. by the time you are writing code in the webforms view engine there should not be a need to access this object.

there are better ways of extracting the email addresses, how they are stored would depend on the best approach to get these values.

FindControl is another lower level function. it's useful when you are search within controls like repeaters, listsviews, etc but not much beyond that.

there isn't any value in looping 1 throuhg 5 to find a control. not only are your controls poorly named (label1, 2, n) but readability within your code is lost.

you would be better off access the label object directly.
if you want to use find control then you need to confirm the label does infact exists before accessing it's properties.
Code:
var label = FindControl("...") as Label;
if(label != null)
{
   label.Text = "hello world";
}

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Jason,

I'm requesting a response from a third party API. I'm parsing out the email addresses from that respone. Then I'm trying to count the number of emails and display them in a label with that also includes the type of emails they are. There are 5 types of emails, hence the label1 - 5 naming. So the labels would show something like this:

Bounces: 18
Opens: 25
Sends: 125
Forwards: 0
Optouts; 1

There is a combo box that is loaded with the 5 uri's for these email types. I click a button and the code loops through the items (uri's) in the combo box and makes a request from the remote server.

It's not production code yet, but I do appreciat very much you looking at it.

Thank you,
 
so your doing a screen scrape to extract the emails? i would abstract this into a separate object altogether to encapsulate the implementation details.

as for you UI; this is where you can clean up the code. I would explicitly structure your UI elements in a manner that is easy to understand.

for example create a BouncesLabel, OpenLabel, etc. get the values form the results of the screen scrape and set the values of these controls.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top