Click here to Skip to main content
15,903,523 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

I got this exception: "object reference not set to an instance of an object".
Why did this exception occur?

Can you please suggest me?
C#
private void AssignmentCollector()
   {
       try
       {
           if (Request.QueryString["flag"] == "1")
           {
               EmployeeDataset = (DataSet)Session["DataSet"];
               if (EmployeeDataset != null)
               {
                   GvEmpDetails.DataSource = "";
                   GvEmpDetails.DataSource = EmployeeDataset;
                   GvEmpDetails.DataBind();
               }
               //Checking the Matching Investigators from Case assignment
               //ArrayList emp = new ArrayList();
               //emp = (ArrayList)Session["empId"];
               empName = (string)Session["employeName"];
               email = (string)Session["Employeemail"];
               string[] emplist = null;
               if (empName != null)
               {
                   emplist = empName.Split(',');
               }
               //Retrieve Rowid
               int rowid = 0;
               gridpagesize = GvEmpDetails.PageSize;
               sessionPageIndex = (int)Session["PageIndex"];
               startIndex = sessionPageIndex * gridpagesize;

              for (int i = 0; i < emplist.Length; i = i + 1)// at this line i am getting that exception.after coming here it is going to catch block
                 {
                   rowid = 0;
                   for (int j = startIndex; j < (startIndex + GvEmpDetails.Rows.Count); j++)
                   {
                       //string id = EmployeeDataset.Tables[0].Rows[j][0].ToString();
                       CheckBox empCheckBox = (CheckBox)GvEmpDetails.Rows[rowid].Cells[0].FindControl("EmployeeCheckBox");
                       Label employeename = (Label)GvEmpDetails.Rows[rowid].Cells[1].FindControl("lblFName");
                       if (emplist[i].ToString() == employeename.Text)
                       {
                           empCheckBox.Checked = true;
                           EmployeeDataset.Tables[0].Rows[j]["Checked"] = "Checked";
                           //empEmail+= emp[i+2].ToString() + ",";
                           if (empName.Contains(emplist[i].ToString()))
                           {
                           }
                           else
                           {
                               empName += emplist[i].ToString() + ",";
                               email += EmployeeDataset.Tables[0].Rows[j]["EmailId"].ToString() + ",";
                           }
                       }
                       rowid++;
                   }

               }
               Session["DataSet"] = EmployeeDataset;
               Session["employeName"] = empName;
               Session["Employeemail"] = email;
           }
           else if (Request.QueryString["flag"] == "2")
           {
               GrpDataset = (DataSet)Session["GrpdSet"];
               if (GrpDataset != null)
               {
                   gvGroupMailId.DataSource = "";
                   gvGroupMailId.DataSource = GrpDataset;
                   gvGroupMailId.DataBind();
               }
               empName = (string)Session["empGrpName"];
               string[] emplist = null;
               if (empName != null)
               {
                   emplist = empName.Split(',');

                   int rowid = 0;
                   gridpagesize = gvGroupMailId.PageSize;
                   sessionPageIndex = (int)Session["GPageIndex"];
                   startIndex = sessionPageIndex * gridpagesize;
                   for (int i = 0; i < emplist.Length; i = i + 1)
                   {
                       rowid = 0;
                       for (int j = startIndex; j < (startIndex + gvGroupMailId.Rows.Count); j++)
                       {
                           //string id = EmployeeDataset.Tables[0].Rows[j][0].ToString();
                           CheckBox empCheckBox = (CheckBox)gvGroupMailId.Rows[rowid].Cells[0].FindControl("EmployeeCheckBox");
                           Label employeename = (Label)gvGroupMailId.Rows[rowid].Cells[1].FindControl("lblGroupName");
                           if (emplist[i].ToString() == employeename.Text)
                           {
                               empCheckBox.Checked = true;
                               GrpDataset.Tables[0].Rows[j]["Checked"] = "Checked";
                               //empEmail+= emp[i+2].ToString() + ",";
                               if (empName.Contains(emplist[i].ToString()))
                               {
                               }
                               else
                               {
                                   empName += emplist[i].ToString() + ",";
                                   email += GrpDataset.Tables[0].Rows[j]["EmailId"].ToString() + ",";
                               }
                           }
                           rowid++;
                       }
                   }
                   Session["GrpdSet"] = GrpDataset;
                   Session["empGrpName"] = empName;
                   Session["employeeGrp"] = email;
               }
           }
       }

Hi all,

please check this code. In this I am getting the exception that I mentioned above.
Please suggest me.
Posted
Updated 8-Feb-11 22:42pm
v5
Comments
Monjurul Habib 8-Feb-11 14:27pm    
please provide a sample code.
mandarapu 9-Feb-11 2:48am    
hi i provide the code and i mentioned where error is coming please check and suggest me
JF2015 9-Feb-11 4:43am    
Edited to add code formatting.

You will get this error when you try an access an object that has not been instatiated.

For e.g. I have a class Employee which has a Name attribute.
Employee obj;
MessageBox.Show(obj.Name.ToString()); //will throw this error as obj is still null


What you should be doing is instantiating your object using the new keyword.
 
Share this answer
 
v3
You're trying to call a method or access a property of an object that is null.

eg

C#
MyObject myObject = null;
myObject.MyMethod(); // error thrown here


Run the code through the debugger and you'll see it.
 
Share this answer
 
It looks like emplist.Length makes the exception happen.
You're strongly encouraged to use the debugger to step into your code (check, for instance, the runtime value of empName).
:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900