2

I'm developing a webapi in Asp Core 3.1. I have a class like this:

public class Employee
{
    public string name { get; set; }
    public string surname { get; set; }
    public List<Contact> contacts { get; set; }
}

in the Get action inside the EmployeeController I'm doing something like this:

var emp = new DTO.EmployeeDetails();
emp = await getEmployeeBaseData(id);

if (emp == null)
    return NotFound();

emp.contacs = await getContats(id);

The problem comes when the employee has no contats:

System.NullReferenceException: Object reference not set to an instance of an object.

I tried initializing the List like:

emp.contacs = new List<Contact>();
emp.contacs = await getContats(id);

The only things actually working is:

try
{
    emp.contacs = await getContats(id);
}
catch (NullReferenceException)
{
    Console.WriteLine("No contacs for id: " + id);
}

Which is very ugly.

I can't assign null to a list. I can't assign an empty enumerable. what can I do?

edit: Sorry if I wrote the question too abstract or generic. This is the getcontacs method:

private async Task<List<Contact>> getContats(int? id)
{
    return await DAL.DataAccess.LoadDataAsync<Contact>(_ConnString,
        "SELECT * FROM contacs WHERE id = @id",
        new { id = id });
}

And this is the LoadDataAsync used inside of it (it's a simple DAL using Dapper):

public static async Task<List<T>> LoadDataAsync<T>(string conn, string sql, T data)
{
    using (IDbConnection cn = new SqlConnection(conn))
    {
        cn.Open();
        var res = await cn.QueryAsync<T>(sql, data);
        return res.ToList();
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Fabio Maccari
  • 57
  • 1
  • 2
  • 7
  • 2
    It's not at all clear to me that *any of the code currently in the question* is the *actual piece of code throwing that exception*. It's difficult to diagnose these issues when we can't see the code. Please create a [mcve]. – Damien_The_Unbeliever Jul 23 '21 at 06:24
  • 1
    `I can't assign null to a list` actually you can. However I would prefer returning an empty list in your getContacts method if there aren't any contacts. Also I don't really get the problem, could you share the content of the getContacts method? The nullreference exception is being thrown in that mehtod, so without it we can't help you – NoConnection Jul 23 '21 at 06:27
  • Your `getContats(id)` should return `Enumerable.Empty()` instead of `null` if there is no contacts. It allow you to use '`Any()` method and omit `NullReferenceException`. – 1_bug Jul 23 '21 at 06:31
  • If I understand your problem correctly you expect an empty list if there are no contacts, but your are getting null instead. This sometimes happens when deserializing. But fix might depend on the serialization library used, and I'm not sure what serialization library is used by default. It might be useful to setup a unit test for just testing serialization/deserialization. – JonasH Jul 23 '21 at 06:48
  • The code you posted cannot throw this exception in the way you suspect. I'll close it as a duplicate so you can find out what is actually causing your exception and fix *that*. – nvoigt Jul 23 '21 at 07:08

0 Answers0