Вопрос

Please see the code below

1. persons = Items.Select(item => componentResolver.ResolvePerson(new TridionUri(item.Id))).ToList();
2. persons.Each(person => person.AdditionalInfo); // gives null reference exception

ResolvePerson looks like:

public Person ResolvePerson(TridionUri personUri)
{
    Person person = publicationResolverService.GetPerson(personUri);
    if (author != null)
    {
        person.Id = personUri.ItemId.ToString();
    }
    return person;
}

Now the problem is that, 'Items' contain an ID for which person is not there. So my 'ResolvePerson' returns a null. This leads to an exception in line#2. I only have control to ResolvePerson method. Is there a way I can skip the personUri for which no person is there instead of returning null?

Это было полезно?

Решение 2

You should filter out the null items before calling the second line using a where clause. But if you can't control that code like you said you can return a "fake" Person that knows it's fake and does nothing when you call AdditionalInfo

Person person = publicationResolverService.GetPerson(personUri);
if (author != null)
{
    person.Id = personUri.ItemId.ToString();
}
else
{
    person = _fakePerson;
}
return person;

Другие советы

You can use Where from Linq to filter null persons

persons.Where(person => person != null).Each(person => person.AdditionalInfo);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top