GET Service - System.NullReferenceException: 'Object reference not set to an instance of an object.'

0

I'm working on creating a Get service in ASP.NET Core, my project contains the following structure:

Certiao.Data - Class Library

Inside it has Dependences , Models : Sexo.cs and DB_SDO_DEVContext.cs

DB_SDO_DEVContext.cs

public DB_SDO_DEVContext(DbContextOptions<DB_SDO_DEVContext> options)
        : base(options)
    {
    }

Sex.cs

public partial class Sexo
{
    public Sexo()
    {
        Pesvtmocrctdatd = new HashSet<Pesvtmocrctdatd>();
    }

    public short Id { get; set; }
    public string Descricao { get; set; }
    public string Abreviacao { get; set; }

    public ICollection<Pesvtmocrctdatd> Pesvtmocrctdatd { get; set; }
}

Repositories > Interfaces: ISexoRepository.cs

{
public interface ISexoRepository
{
    IEnumerable<Sexo> ConsultaSexoIds(List<int> ids);
}
}

SexoRepository.cs

    public class SexoRepository : RepositoryBase<Sexo>, ISexoRepository
    {
        public IEnumerable<Sexo> ConsultaSexoIds(IEnumerable<int> ids)
        {
            using (var Db = new DB_SDO_DEVContext())
            {
                return (from sexo in Db.Sexo
                        where ids.Contains(sexo.Id)
                        select sexo).ToList();
            }
        }
    }
}

SexController.cs

{
[Route("api/[controller]")]
[ApiController]

public class SexoController : ControllerBase
{
    private readonly DB_SDO_DEVContext contexto;

    [HttpGet]
    public ActionResult<List<Sexo>> GetAll()
    {
        return contexto.Sexo.ToList();
    }

}
}

An error occurs when it arrives at SexoController , as in the image below:

Why did you make this mistake in my implementation? I'm just trying to get data from the Sexo table of SQL Server when trying to run my GET method.

    
asked by anonymous 18.10.2018 / 23:46

2 answers

0

Thank you for helping me about the object instance, but the simplest way to bring the data from the sex table when I ran the Get service in my SexoController, was as follows:

{
[Route("api/[controller]")]
[ApiController]

public class SexoController : ControllerBase
{
    private readonly DB_SDO_DEVContext _contexto = new DB_SDO_DEVContext();

    [HttpGet]
    public ActionResult<List<Sexo>> GetAll()
    {
            return _contexto.Sexo.ToList();
    }

}

}

Now, using the Postman program, the code was traversed and the result came down:

[
{
    "id": 1,
    "descricao": "Masculino",
    "abreviacao": "M",
    "pesvtmocrctdatd": []
},
{
    "id": 2,
    "descricao": "Feminino",
    "abreviacao": "F",
    "pesvtmocrctdatd": []
},
{
    "id": 3,
    "descricao": "Indeterminado",
    "abreviacao": "I",
    "pesvtmocrctdatd": []
}

]

    
19.10.2018 / 16:40
1

The error is self explanatory, you are trying to access an object that was not instantiated, so its value is null, there is nothing that can do with it and so it gives an error.

The solution to this is to initialize the object, and only you know how to do it according to the need of your application, but a way that will avoid this error, even if it causes another one to go forward would be:

public class SexoController : ControllerBase {
    private readonly DB_SDO_DEVContext contexto;
    [HttpGet]
    public ActionResult<List<Sexo>> GetAll() {
        var optionsBuilder = new DbContextOptionsBuilder<DB_SDO_DEVContext>();
        contexto = new DB_SDO_DEVContext(optionsBuilder.Options)
        return contexto.Sexo.ToList();
    }
}

This will not only give a problem because the execution is ephemeral, in other conditions it should release the resource, as it did in the repository (which I do not know if it should be created like this).

In this way you are assigning a value with an object of this class. The new creates the object.

Of course, if it does not work as it does it needs to learn all the details of the tool you are using.

Alias this is one of the worst class names I've ever seen.

    
19.10.2018 / 00:16