views:

52

answers:

3

I am getting the following error while i submit a form after i added a dropdown box in my designer

System.NullReferenceException: Object reference not set to an instance of an object. at WebApplication1._Default.collectEmailBodyText() in C:\v1.5_production_05June09\Default.aspx.vb:line 219

Below is the extra two lines that i added in collectEmailBodyText()

tempPanelDropDownBox = DirectCast(form1.FindControl(("txt_" & panelUsed & "_ddinput") + counter.ToString()), DropDownList)

tempCollector = tempCollector + ": " + tempPanelDropDownBox.SelectedItem.Text

+2  A: 

I would guess the most likely issue there is that form1.FindControl is not finding the control. I would recommend performing the find control first, then checking it's not null before attempting to access any values from it.

Sohnee
+2  A: 

As the exception says one of the objects has a null value. The easiest thing is to set a break point on the line that you're getting the exception on and use something like QuickWatch to evaluate the different sections of the line to discover which is returning null.

The most obvious one is the FindControl returning null.

Mark
+2  A: 

As the other posters have said, it seems likely that the call to the FindControl method is returning Nothing (null), so then trying to access a property like SelectedItem will cause the NullReferenceException.

Your code FindControl("txt_" & panelUsed & "_ddinput") + counter.ToString()) is trying to find a drop-down list with a server ID of some strings concatenated together and then what looks like a variable number at the end. This seems a bit odd; do you really want the number at the end? I'd expect something like that when using dynamically-added controls.

Graham Clark