NullReferenceException控件。添加c#错误

NullReferenceException控件。添加c#错误,c#,controls,nullreferenceexception,C#,Controls,Nullreferenceexception,在我开始调试我的程序后,我遇到了以下错误。你知道怎么解决这个问题吗 System.NullReferenceException was unhandled by user code Message="Object reference not set to an instance of an object." 这是指: pnlDropDownList.Controls.Add(ddl); 在CreateDropDownList方法中。显然,ddl必须是一个空对象,即使我之前在同一个方法中初始化

在我开始调试我的程序后,我遇到了以下错误。你知道怎么解决这个问题吗

System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."
这是指:

pnlDropDownList.Controls.Add(ddl);
在CreateDropDownList方法中。显然,ddl必须是一个空对象,即使我之前在同一个方法中初始化了ddl。你明白我为什么会收到这个错误吗?请参阅下面的代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ADONET_namespace;
using MatrixApp;

namespace AddFileToSQL
{
public partial class DataMatch : _Default
{

protected System.Web.UI.WebControls.PlaceHolder phTextBoxes;
protected System.Web.UI.WebControls.PlaceHolder phDropDownLists;
protected System.Web.UI.WebControls.Button btnAnotherRequest;
protected System.Web.UI.WebControls.Panel pnlCreateData;
protected System.Web.UI.WebControls.Literal lTextData;
protected System.Web.UI.WebControls.Panel pnlDisplayData;
Panel pnlDropDownList;
protected static string inputfile2;
static string[] headers = null;
static string[] data = null;
static string[] data2 = null;
static DataTable myInputFile = new DataTable("MyInputFile");
static string[] myUserSelections;

// a Property that manages a counter stored in ViewState
protected int NumberOfControls
{
get { return (int)ViewState["NumControls"]; }
set { ViewState["NumControls"] = value; }
}

public void EditRecord(object recordID)
{
SelectedRecordID = recordID;
// Load record from database and show in control
}

protected object SelectedRecordID
{
get
{
return ViewState["SelectedRecordID"];
}

set
{
ViewState["SelectedRecordID"] = value;
}
}

protected void OnPreLoad(object sender, EventArgs e)
{

//Create a Dynamic Panel
pnlDropDownList = new Panel();
pnlDropDownList.ID = "pnlDropDownList";
pnlDropDownList.BorderWidth = 1;
pnlDropDownList.Width = 300;
this.form1.Controls.Add(pnlDropDownList);
}

// Page Load 
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
this.NumberOfControls = 0;
}
}

// Add DropDownList Control to Placeholder
private void CreateDropDownLists()
{
for (int counter = 0; counter < NumberOfControls; counter++)
{
DropDownList ddl = new DropDownList();
SqlDataReader dr = ADONET_methods.DisplayTableColumns(targettable);
ddl.ID = "DropDownListID" + (counter + 1).ToString();
ddl.DataTextField = "COLUMN_NAME";
ddl.DataValueField = "COLUMN_NAME";
ddl.DataSource = dr;
ddl.DataBind();
ddl.AutoPostBack = true;
ddl.EnableViewState = true; //Preserves View State info on Postbacks
ddl.Style["position"] = "absolute";
ddl.Style["top"] = 100 * counter + 80 + "px";
ddl.Style["left"] = 250 + "px";
ddl.SelectedIndexChanged += new EventHandler(this.OnSelectedIndexChanged);
pnlDropDownList.Controls.Add(ddl);
pnlDropDownList.Controls.Add(new LiteralControl("<br><br><br>"));
dr.Close();
}
}

private void CreateLabels()
{
for (int counter = 0; counter < NumberOfControls; counter++)
{
Label lbl = new Label();
lbl.ID = "Label" + counter.ToString();
lbl.Text = headers[counter];
lbl.Style["position"] = "absolute";
lbl.Style["top"] = 100 * counter + 50 + "px";
lbl.Style["left"] = 250 + "px";
pnlDropDownList.Controls.Add(lbl);
pnlDropDownList.Controls.Add(new LiteralControl("<br><br><br>"));
}
}

// Add TextBoxes Control to Placeholder
private void RecreateDropDownLists()
{
for (int counter = 0; counter < NumberOfControls; counter++)
{
DropDownList ddl = new DropDownList();
SqlDataReader dr = ADONET_methods.DisplayTableColumns(targettable);
ddl.ID = "DropDownListID" + (counter + 1).ToString();
ddl.DataTextField = "COLUMN_NAME";
ddl.DataValueField = "COLUMN_NAME";
ddl.DataSource = dr;
ddl.DataBind();
myUserSelections[counter] = "";
dr.Close();
ddl.AutoPostBack = true;
ddl.EnableViewState = false; //Preserves View State info on Postbacks
ddl.Style["position"] = "absolute";
ddl.Style["top"] = 100 * counter + 80 + "px";
ddl.Style["left"] = 250 + "px";
pnlDropDownList.Controls.Add(ddl);
pnlDropDownList.Controls.Add(new LiteralControl("<br><br><br>"));
}
}

// Create TextBoxes and DropDownList data here on postback.
protected override void CreateChildControls()
{
// create the child controls if the server control does not contains child controls
this.EnsureChildControls();
// Creates a new ControlCollection. 
this.CreateControlCollection();
// Here we are recreating controls to persist the ViewState on every post back

if (Page.IsPostBack)
{
RecreateDropDownLists();
RecreateLabels();
}

// Create these conrols when asp.net page is created
else
{
PopulateFileInputTable();
CreateDropDownLists();
CreateLabels();
}

// Prevent child controls from being created again.
this.ChildControlsCreated = true;
}

private void AppendRecords()
{
switch (targettable)
{
case "ContactType":
for (int r = 0; r < myInputFile.Rows.Count; r++)
{ ADONET_methods.AppendDataCT(myInputFile.Rows[r]); }
break;
case "Contact":
for (int r = 0; r < myInputFile.Rows.Count; r++)
{ ADONET_methods.AppendDataC(myInputFile.Rows[r]); }
break;
case "AddressType":
for (int r = 0; r < myInputFile.Rows.Count; r++)
{ ADONET_methods.AppendDataAT(myInputFile.Rows[r]); }
break;

default: throw new ArgumentOutOfRangeException("targettable type", targettable);
}
}

// Read all the data from TextBoxes and DropDownLists 
protected void btnSubmit_Click(object sender, System.EventArgs e)
{
int cnt = FindOccurence("DropDownListID");
EditRecord("DropDownListID" + Convert.ToString(cnt + 1));
AppendRecords();
pnlDisplayData.Visible = false;
}

private int FindOccurence(string substr)
{
string reqstr = Request.Form.ToString();
return ((reqstr.Length - reqstr.Replace(substr, "").Length) / substr.Length);
}

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
(this.btnSubmit_Click);
}

#endregion

}
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用系统数据;
使用System.Data.SqlClient;
使用系统诊断;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用ADONET_名称空间;
使用MatrixApp;
命名空间AddFileToSQL
{
公共部分类数据匹配:\u默认值
{
受保护的System.Web.UI.WebControl.PlaceHolder phTextBox;
受保护的System.Web.UI.WebControls.PlaceHolder phDropDownLists;
受保护的System.Web.UI.WebControls.Button btnAnotherRequest;
受保护的System.Web.UI.WebControl.Panel pnlCreateData;
受保护的System.Web.UI.WebControls.Literal lTextData;
受保护的System.Web.UI.WebControls.Panel pnlDisplayData;
面板pnlDropDownList;
受保护的静态字符串inputfile2;
静态字符串[]头=null;
静态字符串[]数据=null;
静态字符串[]数据2=null;
静态数据表myInputFile=新数据表(“myInputFile”);
静态字符串[]myUserSelections;
//管理存储在ViewState中的计数器的属性
受保护的整数控制
{
获取{return(int)ViewState[“NumControls”];}
设置{ViewState[“NumControls”]=value;}
}
公共无效编辑记录(对象记录ID)
{
SelectedRecordID=recordID;
//从数据库加载记录并在控件中显示
}
受保护的对象SelectedRecordID
{
得到
{
返回视图状态[“SelectedRecordID”];
}
设置
{
ViewState[“SelectedRecordID”]=值;
}
}
受保护的void OnPreLoad(对象发送方,事件参数e)
{
//创建一个动态面板
pnlDropDownList=新面板();
pnlDropDownList.ID=“pnlDropDownList”;
pnlDropDownList.BorderWidth=1;
pnlDropDownList.宽度=300;
this.form1.Controls.Add(pnlDropDownList);
}
//页面加载
私有无效页面加载(对象发送方,System.EventArgs e)
{
如果(!Page.IsPostBack)
{
此.NumberOfControls=0;
}
}
//将DropDownList控件添加到占位符
私有void CreateDropDownLists()
{
用于(int计数器=0;计数器

”); Close博士(); } } 私有void CreateLabels() { 用于(int计数器=0;计数器

”); } } //将文本框控件添加到占位符 私有void重新创建dropdownlists() { 用于(int计数器=0;计数器

”); } } //在回发时在此处创建文本框和下拉列表数据。 受保护的覆盖无效CreateChildControls() { //如果服务器控件不包含子控件,则创建子控件 this.ensureChildControl(); //创建一个新的ControlCollection。 这是.CreateControlCollection(); //在这里,我们正在重新创建控件,以便在每次回发时保持ViewState 如果(第IsPostBack页) { 重新创建dropdownlists(); 重新创建标签(); } //在创建asp.net页面时创建这些控件 其他的 { populateFileInputable(); CreateDropDownLists(); CreateLabels(); } //防止再次创建子控件。 this.ChildControlsCreated=true; } 私人档案() { 开关(可锁定) { 案例“ContactType”: 对于(int r=0;r
我想是referrin