0

I have two List. Which content simple string values. As example:

IList1 content:

  • The Avengers
  • Shutter Island
  • Inception
  • The Dark Knight Rises

List2 content:

  • The Avengers
  • Shutter Island
  • Inception
  • The Dark Knight Rises
  • Parks and Recreation
  • Scandal

I want to compare two list and it will return mismatch value. Like in this case it will return "Parks and Recreation" and "Scandal" as they are not matching with the values of List1.

I tried it. But it throws exception "Object reference not set to an instance of an object".

static void Main(string[] args)
    {
        List<string> list1 = new List<string>();
        list1.Add("The Avengers");
        list1.Add("Shutter Island");
        list1.Add("Inception");
        list1.Add("The Dark Knight Rises");

        List<string> list2 = new List<string>();
        list2.Add("The Avengers");
        list2.Add("Shutter Island");
        list2.Add("Inception");
        list2.Add("The Dark Knight Rises");
        list2.Add("Parks and Recreation");
        list2.Add("Scandal");    

       try
        {
            List<string> difference = Comparator(list1, list2);
            foreach (var value in difference)
            {
                Console.WriteLine(value);
            }
        }
        catch (System.NullReferenceException e)
        {
            Console.WriteLine(e.Message);    
        }

        Console.ReadLine();

    }
    public static List<string> Comparator(List<string> list1, List<string> list2)
    {
        IEnumerable<string> differenceQuery = list1.Except(list2);
        List<string> differ = null;

        foreach (string s in differenceQuery)
            differ.Add(s);

        return differ;
    }

Can anyone help me out? Thanks in advance.

Anudeep
  • 337
  • 2
  • 4
  • 13
  • 1
    `List differ = new List();` – Yahya Apr 18 '13 at 13:58
  • 1
    Inside the Comparator method, change List differ = null; for List differ = new List(); – Tiago B Apr 18 '13 at 13:59
  • `List differ = null;` // here is your error come from. – hazzik Apr 18 '13 at 13:59
  • Is the order, or number of occurrences, of the elements relevant to the desired result? – Tormod Apr 18 '13 at 14:10
  • List differ = new List(); – Kira Apr 18 '13 at 14:11
  • I would suggest reading up on how to debug code and how to use your IDE. This is about as simple as it gets when trying to track down an error. Any debugger will give you the line number where the error is occurring and you already know the type of the exception, so figuring out what the problem is should be straight forward. – Brent Stewart Apr 18 '13 at 16:08

4 Answers4

8

Except method does exactly what you described, so just call ToList to get results in a List<T>:

return list2.Except(list1).ToList();

Your code throws NullReffereceException becausediffer variable is not initialized:

    List<string> differ = null;

change it to:

    List<string> differ = new List<string>();

However, I would suggest using ToList(). YouComparator method won't be necessary than:

        List<string> difference = list2.Except(list1).ToList()
        foreach (var value in difference)
        {
            Console.WriteLine(value);
        }

Update - whole code, working as expected

static void Main(string[] args)
{
    List<string> list1 = new List<string>();
    list1.Add("The Avengers");
    list1.Add("Shutter Island");
    list1.Add("Inception");
    list1.Add("The Dark Knight Rises");

    List<string> list2 = new List<string>();
    list2.Add("The Avengers");
    list2.Add("Shutter Island");
    list2.Add("Inception");
    list2.Add("The Dark Knight Rises");
    list2.Add("Parks and Recreation");
    list2.Add("Scandal");

    List<string> difference = list2.Except(list1).ToList();
    foreach (var value in difference)
    {
        Console.WriteLine(value);
    }

    Console.ReadLine();
}

Result

Parks and Recreation
Scandal
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
5

The problem is with this line:

List<string> differ = null;

After that you try to add something to the list, but the list is null. There's your exception.

You could change it as follows:

List<string> differ = new List<string>();

But easier is to replace this line:

List<string> difference = Comparator(list1, list2);

By:

List<string> difference = list1.Except(list2).ToList();
Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
1

Use Except

var difference = list1.Except(list2).ToList();
Praveen Nambiar
  • 4,852
  • 1
  • 22
  • 31
1

The problem is right here.

List<string> differ = null;

Needs to be

List<string> differ = new List<string>();
cgotberg
  • 2,045
  • 1
  • 18
  • 18