As the title says, I have a method that throws a NullReferenceException in my DataGridView Binding method.
Here is my code (simplified):
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);
}
The change header Method:
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;
}
This is the stack trace I get:
[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
From what I understood of it, the error comes from theChangeHeaders method. I tried putting the ChangeHeaders method in comments and there were no more errors. That told me the error was really from the ChangeHeaders method. However, it is the first time that I get a NUllReferenceException from a Method and I am lost as to why it is doing that. Specially that the said method is in the code and there are no typo when I call it.
How can I fix this, thanks.
Check the value of _docTypeQueryStr.
Probably you are passing a null value to ChangeHeaders and doing a ToUpper().