vendredi 24 juin 2016

C# Reflection Additional information: Object reference not set to an instance of an object. (NullReferenceException)

I'm working on stream data operators and need to check if object from List haves field named like content

string field1

public ResultList joinLists<T, U>(List<T> categories, List<U> products, string field1, string field2)
    {
        ResultList resList = new ResultList();
        bool headeres = false;

            List<T> result1 = new List<T>();
            foreach (var cat in categories)
            {
                if (cat != null)
                {
                var name = cat.GetType().GetField(field1, BindingFlags.DeclaredOnly |
                                  BindingFlags.Public | BindingFlags.NonPublic |
                                  BindingFlags.Instance | BindingFlags.GetField).Name;
                    if (name == field1)
                    {
                        result1.Add(cat);
                    }
                }
            }

Debugger shows that exception appears on line

var name = cat.GetType().GetField.....

I've looked for that problem for quite a bit of time and i really don't know what to do, I've double checked if good List is passed, so as if field1 corresponds to that exect object type, everything looks fine, but it'll probably be some hyper newbie mistake.

Thanks for reading & comments





Aucun commentaire:

Enregistrer un commentaire