Hi can anyone tell me why I'm getting a empty list back when executing this entity framework query?

Socks93

Active member
Joined
Oct 22, 2021
Messages
29
Programming Experience
Beginner
See code below:

C#:
 public ActionResult<List<string>> FetchConfig([FromBody] FetchConfigDto fetchConfigDto)
        {
           
            List<string> myResult = new List<string>();
            _logger.Debug("getting config from database");

            try
            {
                myResult = _context.config.Where(x => x.SwitchID == fetchConfigDto.SwitchID && x.AgentId == fetchConfigDto.AgentId).Select(x => x.ConfigFile).ToList();
               
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Error fetching data from Config table");
            }
            return myResult;
 }


This is the error I'm receiving:

Error fetching data from Config table|Object reference not set to an instance of an object.


Any help would be much appreciated!
 
You are getting an empty list back because an exception got thrown, and you decided to log the exception and return the empty list that you created on line 4.

Now if your question was why are you getting an exception, you need to inspect all the variables and properties on line 9. Which one of them is null?
 
Last edited:
Back
Top Bottom