2

Just starting out and need all the help. The below code won't run. Error msg says "reference not set to the instance of an object", and it points to the employee reference in the WriteLine method. Kindly assist

class Program
{
    static void Main(string[] args)
    {
        List<Employee> empList = new List<Employee>()
        { 
           new Employee { ID = 101, Salary = 6000000, Name = "Jane" },
           new Employee{ ID = 102, Salary = 6000000, Name = "Jane" },
           new Employee { ID = 103, Salary = 6000000, Name = "James" },
           new Employee{ ID = 104, Salary = 6000000, Name = "Jasmie" },
           new Employee { ID = 105, Salary = 6000000, Name = "Janet" },
        };

        Predicate<Employee> emPredicate = new Predicate<Employee>(getEmpName);
        Employee employee = empList.Find(emp=>  emPredicate(emp)); 

        Console.WriteLine(" ID = {0}, Name = {1}",employee.ID,employee.Name );
        Console.ReadLine();
    }

    public static bool getEmpName(Employee em)
    {
        return em.ID == 002;
    }
}

class Employee 
{
    public int ID { get; set; }
    public int Salary { get; set; }
    public string Name { get; set; }
}
Yuval Itzchakov
  • 136,303
  • 28
  • 230
  • 296
Doo
  • 31
  • 4
  • It looks like you just Fat Fingered the 0 instead of the 1 in your getEmpName(). Did you mean to type 102 instead of 002? – Spencer Waz Jul 25 '14 at 13:21
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Daniel Kelley Jul 25 '14 at 13:22
  • Thanks a bunch. Entered 102 and it sure compiled. I must try n get a handle on the basics and the details. – Doo Jul 25 '14 at 13:44

2 Answers2

5
  1. If your program throw an exception at runtime then it means it does compile

  2. There is no employee with ID 002. That's why the Find method returns null and you are getting the NullReferenceException.

  3. I would use more appropriate names for my methods. For example getEmpName doesn't return a name it returns a bool, that makes your predicate a bit confusing. You can name it GetEmployeeById and you can add an id parameter to your method then it makes some sense.You can also just use:

    Employee employee = empList.Find(emp => emp.ID == 2); 
    

If you just want to find the employee that has the ID 2.

Selman Genç
  • 94,267
  • 13
  • 106
  • 172
  • Thanks for the advice. Entered 102 and it sure compiled. I will work on naming like you said. Wish I could understand the Exception messages better. Thanks again – Doo Jul 25 '14 at 13:49
4

From List<T>.Find:

Return Value Type: T

The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T.

It isn't finding a match, hence returning default(T), which is null. Add a nullity before Console.WriteLine, and of course, correct your getEmpName predicate (I'm assuming you ment to check em.ID == 102):

if (employee != null)
{
    Console.WriteLine(" ID = {0}, Name = {1}",employee.ID,employee.Name );
}
Yuval Itzchakov
  • 136,303
  • 28
  • 230
  • 296
  • Thanks a bunch, especially for that bit about the nullity. i have seen it before but it makes more sense now that I need to use it. The way you explained the Find method, that's deep. Any materials that can help me out with stuff like this? Thanks again! – Doo Jul 25 '14 at 14:02
  • Every documentation you need on any method, search on [MSDN](http://msdn.microsoft.com/en-US/), its all there :) – Yuval Itzchakov Jul 25 '14 at 14:05
  • Note also that you should mark an accepted answer, if any of the supplied answers helped you with you problem. To do that, check the empty check mark right under the voting meter of the answer, once you click it, it will turn green. – Yuval Itzchakov Jul 25 '14 at 14:08
  • Thanks. Will check that out! – Doo Jul 25 '14 at 14:08