GET service - System.Nullreferenceexception: 'Object Reference not set to an instance of an Object. '

Asked

Viewed 1,938 times

1

I am acting in the creation of a service Get in ASP.NET Core, my project contains the following structure:

Certidao.Data - Class Library

Inside there is 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();
            }
        }
    }
}

Sexocontroller.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 in SexoController, as in the image below:

Visual Studio com o erro

Why did you give this error in my implementation? I’m just trying to bring data from the table Sexo SQL Server when trying to run my method GET.

2 answers

1

The error is self-explanatory, you are trying to access an object that has not been instantiated, then its value is null, there is nothing you can do with it and so 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 one way that will avoid this error, even if it causes another 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();
    }
}

I put in the Github for future reference.

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

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

Of course, if it doesn’t work the way you want it, you need to learn all the details of the tool you are using.

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

1


thanks for helping me with the object instance, but the simplest way to bring 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 yes, using the program Postman, 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": []
}

]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.