正如标题所说,我有一个方法在我的DataGridView Binding方法中抛出NullReferenceException。
这是我的代码(简化):
private void GridViewBinding(string sortExpression, string direction)
{
string lang = string.Empty;
Regex appnoRegex = new Regex("\\d{4}(-)\\d{4}(-)\\d{1}");
int colCount = gvData.Columns.Count;
DataView dv = GetData();
//DataView dvApplication = GetApplicationsData();
//Sorting only on postback, else rel. docs won't appear
if (Page.IsPostBack)
{
dv.Sort = sortExpression + " " + direction;
}
lblTotal.Text = dv.Count.ToString();
gvData.DataSource = dv;
gvData.DataKeyNames = new string[] { "InterventionID", "ItemNumber" };
gvData.DataBind();
ChangeHeaders(_docTypeQueryStr, colCount, appnoRegex);
}
更改标题方法:
private void ChangeHeaders(string DocType, int colCount, Regex appnoRegex)
{
switch (DocType.ToUpper())
{
//intervention documents
case "I":
lblDocumentTypeTitle.Text = Resources.GeneralResouces.lblTypeTitleInterventionsText;
lblIntervenorLastName.Text = Resources.GeneralResouces.lblIntervenorLastNameInterventionText;
break;
//Replies document
case "R":
lblDocumentTypeTitle.Text = Resources.GeneralResouces.lblTypeTitleRepliesText;
lblIntervenorLastName.Text = Resources.GeneralResouces.lblIntervenorLastNameRepliesText;
break;
//Answer Documents
case "AR":
lblDocumentTypeTitle.Text = Resources.GeneralResouces.lblTypeTitleAnswersText;
lblIntervenorLastName.Text = Resources.GeneralResouces.lblIntervenorLastNameAnswerText;
break;
//if none of teh above
default:
//Notice of consultations
if (applicationCount == 0 && !appnoRegex.IsMatch(_eventNoQueryStr))
{
lblIntervenorLastName.Text = Resources.GeneralResouces.lblIntervenorText;
}
//Part 1 applications
else
{
//Part 1 applications and notices with applications
if (applicationCount != 0 || appnoRegex.IsMatch(_eventNoQueryStr))
{
lblIntervenorLastName.Text = Resources.GeneralResouces.lblIntervenorWithApplicationsText;
}
}
break;
}
这是我得到的堆栈跟踪:
[NullReferenceException: Object reference not set to an instance of an object.]
Interventions._Default.ChangeHeaders(String DocType, Int32 colCount, Regex appnoRegex) +13
Interventions._Default.GridViewBinding(String sortExpression, String direction) +331
Interventions._Default.Page_Load(Object sender, EventArgs e) +888
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +91
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
根据我的理解,错误来自theChangeHeaders方法。我尝试将ChangeHeaders方法放在注释中,并且没有更多错误。这告诉我错误实际上来自ChangeHeaders方法。但是,这是我第一次从方法中获得NUllReferenceException并且我迷失了它为什么这样做。特别是所述方法在代码中,并且在我调用时没有拼写错误。
我该如何解决这个问题,谢谢。
答案 0 :(得分:0)
检查_docTypeQueryStr的值。
您可能正在将null值传递给ChangeHeaders并执行ToUpper()。