0

Does anyone know where is my code going wrong here? This piece of code work perfectly fine but I transferred the file to another computer, this error popped out when I compile the file.

 private void ShowGeneratedSchedule(string sLocationName, string sAllocationDate)
    {
        //lstAllocation = db.allocations.Where(a => a.AllocationDate == sAllocationDate &&
        //    a.LocationName == sLocationName).ToList();

        scheduleDGV.EditMode = DataGridViewEditMode.EditProgrammatically;
        for (int i = 0; i < lstAllocation.Count; i++)
        {
            for (int j = 0; j < scheduleDGV.Rows.Count - 1; j++)
            {
                for (int k = 0; k < scheduleDGV.Columns.Count; k++)
                {
                    if (scheduleDGV[0, j].Value.Equals(lstAllocation[i].LocationName) &&
                        scheduleDGV[1, j].Value.Equals(lstAllocation[i].StationName) &&
                        scheduleDGV.Columns[k].HeaderText.Equals(lstAllocation[i].AllocationTime.ToString()))
                    {
                        if (lstAllocation[i].EmployeeName != null)
                        {
                            scheduleDGV[k, j].Value = lstAllocation[i].EmployeeName;
                        }
                    }
                }
            }
        }

        //scheduleDGV.DataSource = lstEmployeeSlot;
    }

The error is displayed when it reaches this line,

for (int i = 0; i < lstAllocation.Count; i++)

Does anyone know what's wrong here? Is it possible that it is transferred into another computer?

5
  • The debugger knows. :P Can you post the exception and stack trace? Commented Aug 27, 2012 at 3:48
  • Where is lstAllocation declared and assigned? From the code provided, it does not show the assignment, which might be why it is null. Use the debugger to confirm if it is null at that stage. Commented Aug 27, 2012 at 3:49
  • lstAllocation declaration is commented. Why? Commented Aug 27, 2012 at 3:49
  • The line should be commented out previously when I transferred this piece of code to another computer. Codes won't auto comment out themselves I reckon? Commented Aug 27, 2012 at 4:01
  • fyi: i have this "List<allocation> lstAllocation;" and not the one I commented out Commented Aug 27, 2012 at 4:04

4 Answers 4

2

lstAllocation is not initialized? What is it with the comment in the first line of the method? Why is that commented out? Without that lstAllocation has no content.

Sign up to request clarification or add additional context in comments.

2 Comments

I have declared this 'List<allocation> lstAllocation;' and not the one that is commented out.
You not only have to declare a variable, but also initialize it when you want to use it. When you declare a variable you specify a container (=datatype) in which you save the object (or rather a reference to the object). Then you actually have to create the object. I assume db.allocations.Where(a => a.AllocationDate == sAllocationDate && a.LocationName == sLocationName).ToList(); retrieves the object that you save in your container. But at the moment you have only created a container that can save the reference to an object - but how haven't actually set the reference.
2

This, most likely, means that lstAllocation is null and was never assigned a value. Why this line is commented out?

 //lstAllocation = db.allocations.Where(a => a.AllocationDate == sAllocationDate &&
        //    a.LocationName == sLocationName).ToList();

Comments

1

Looks like you have the line commented out where you assign lstAllocation to something. This is most likely your problem.

Comments

0

The lstAllocation reference variable is initialized with null (or an object is not constructed yet or an object is missing) somewhere within the program/code.

It will be good practice to check the value of reference variable - whether it has null or reference of an object.

private void ShowGeneratedSchedule(string sLocationName, string sAllocationDate)
    {
        if(lstAllocation==null){
           Console.WriteLine("List is not initialized. Can't proceed");
           return;
        }
        //rest code
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.