2009-06-10 83 views
0

下面的代碼,我必須推薦在我的asp網站中添加下拉菜單。我已經添加了,你可以請檢查什麼是錯的。Dropdown NullReferenceException

Function collectEmailBodyText() 
     Try 
      Dim counterEnd As Integer = subActivated_HowManyControlsInAPanel() 
      Dim counter As Integer = 0 
      Dim tempPanelLabel As Label 
      Dim tempPanelInputBox As TextBox 
      Dim tempPanelDropDownBox As DropDownList 
      Dim tempCollector As String 
      Dim panelUsed As String = "" 
      '* Find out which panel is used to collect panel data: 
      panelUsed = view0_panelUsed.ToString 
      Response.Write("<!-- " + panelUsed + " -->") 
      ' 
      tempCollector = "<p><b>" + lbl_viewTitle0.Text + "</b>" 
      tempCollector = tempCollector + "<br>" + lbl_view0_firstName.Text + ": " + txt_firstName.Text 
      tempCollector = tempCollector + "<br>" + lbl_view0_surname.Text + ": " + txtSurName.Text 
      tempCollector = tempCollector + "<br>" + lbl_view0_ContactNum.Text + ": " + txt_contactNum.Text 
      ' 
      tempCollector = tempCollector + "<p><b>" + lbl_viewTitle1.Text + "</b>" 
      tempCollector = tempCollector + "<br>" + lbl_view1_firstName.Text + ": " + txt_view1_firstname.Text 
      tempCollector = tempCollector + "<br>" + lbl_view1_surname.Text + ": " + txt_view1_surname.Text 
      tempCollector = tempCollector + "<br>" + lbl_view1_userID.Text + " " + txt_view1_userID.Text 
      tempCollector = tempCollector + "<br>" + lbl_view1_workUnit.Text + ": " + ddl_view1_workunit.SelectedItem.Text + " :: " + ddl_view1_workunit.SelectedValue.ToString() 
      tempCollector = tempCollector + "<br>" + lbl_view0_typeOfRequest.Text + ": " + ddl_view0_typeOfRequest.SelectedItem.ToString 
      tempCollector = tempCollector + "<br>" + lbl_view0_workUnitLevel.Text + ": " + ddl_view0_workUnitLevel.SelectedItem.ToString + "<br>" 
      ' 
      '* Collect panel data: 
      Do 
       counter = counter + 1 
       tempPanelLabel = New Label 
       tempPanelInputBox = New TextBox 
       tempPanelDropDownBox = New DropDownList 
       tempPanelLabel = form1.FindControl("lbl_" + panelUsed + "_label" + counter.ToString()) 
       tempPanelInputBox = form1.FindControl("txt_" + panelUsed + "_input" + counter.ToString()) 
       tempPanelDropDownBox = DirectCast(form1.FindControl(("txt_" & panelUsed & "_ddinput") + counter.ToString()), DropDownList) 

       tempCollector = tempCollector + "<br>" + tempPanelLabel.Text 
       'tempCollector = tempCollector + ": " + tempPanelInputBox.Text 
       tempCollector = tempCollector + ": " + tempPanelDropDownBox.SelectedValue 


      Loop Until counter = counterEnd 
      ' 
      If storeSelected() = 0 Then 
       tempCollector = tempCollector + "<p><b>" + lbl_viewTitle2.Text + "</b>" 
       tempCollector = tempCollector + "<br>" + lbl_view2_ManagersEmailAddress.Text + ": " + txt_view2_ManagersEmailAddress.Text 
      End If 
      ' 
      Return tempCollector 
     Catch ex As Exception 
      Return ex.ToString() 
      Response.Write(ex.ToString()) 
     End Try 

    End Function 

下面是多餘的我加

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

,我收到以下錯誤:

System.NullReferenceException:未設置爲一個對象的實例對象引用。在C:\ v1.5_production_05June09 \ Default.aspx.vb中的WebApplication1._Default.collectEmailBodyText()中:第220行

+0

你爲什麼再次發佈這個問題? – Shoban 2009-06-10 07:22:37

+0

我之前沒有發佈代碼..請幫忙 – SmartestVEGA 2009-06-10 07:24:45

回答

1

如果知道哪個對象爲空,則會更容易知道行號,但是猜測其中一行在形式

tempPanelInputBox = form1.FindControl("txt_" + panelUsed + "_input" + counter.ToString()) 

出現故障,這將是因爲由部分產生的名稱「txt_」 + panelUsed +「_Input」 + counter.ToString()不形式內的控制相匹配。

所以,看看文件中的線220,並檢查它正在尋找的表格中首次出現的控制。

看着它,我覺得行

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

應該讀

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

在名稱的差異是DDL不是DD(基於假設你的名字下拉菜單DDL)

1

在你的代碼中,如果一個名爲你串聯的控件不存在(例如,如果你的連接不是很正確),那麼你可能會得到這個錯誤。

首先執行findcontrol函數,並在嘗試從它獲取值之前檢查它是否爲空。

爲了調試的目的,它可能是值得檢查「panelUsed」是你期待什麼。如果它是空字符串,因爲變量沒有被正確設置,這可能會導致你的錯誤。

1

嘗試使用調試器! 一步一步來,你會發現你的錯誤。