简体   繁体   中英

C#, ASP.NET - NullReferenceException - Object reference not set to an instance of an object

Definition of variables in use:

Guid fldProId = (Guid)ffdPro.GetProperty("FieldId");
string fldProValue = (string)ffdPro.GetProperty("FieldValue");
FormFieldDef fmProFldDef = new FormFieldDef();
fmProFldDef.Key = fldProId;
fmProFldDef.Retrieve();
string fldProName = (string)fmProFldDef.GetProperty("FieldName");
string fldProType = (string)fmProFldDef.GetProperty("FieldType");

Lines giving the problem (specifically line 4 (hTxtBox.Text = ...)):

if (fldProType.ToLower() == "textbox")
{
    Label hTxtBox = (Label)findControl(fldProName);
    hTxtBox.Text = fldProValue;
}

All data is gathered from the database correctly, however the label goes screwy. Any ideas?

Are you sure that findControl is returning a value?

Is hTxtBox.Text a property that does any computation on a set that could be throwing the NullReferenceException?

findControl is returning a null value. It could be that the particular Label is not a direct child of the current page, ie, inside an UpdatePanel or some other control so that the actual name of the control is different than the name applied (and thus it can't find it). For example, if it is named "name", the actual name may be ctl0$content$name because it is nested inside another control on the page.

You don't really give enough information about the context for me to give you a better answer.

Looks like fmProFldDef's FieldName property is screwy. Did you verify that it's getting the hTxtBox's client Id?

this line is returning null:

Label hTxtBox = (Label)findControl(fldProName);

It may be a result of "FieldName" not existing (thus this line returning null, then null being used in the lookup)

string fldProName = (string)fmProFldDef.GetProperty("FieldName");

or the text within FieldName not representing a form field.

FindControl可能无法看到文本框 - 它是否在数据绑定控件中(例如ListView,FormView等)?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM