Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Object reference not set to an instance of an object. MVC and Foreign Key

I tried everything and nothing! It’s returning the following message: "System.NullReferenceException: ‘Object reference not set to an instance of an object.’"

Maybe it’s because I use two foreign keys of the same class!? I don’t know… Give me a light, please lol.

 public List<atendimento> ListarAtendimentos()
    {
        List<atendimento> lst = new List<atendimento>();

        using (SqlConnection con = new SqlConnection(cs))
        {
            con.Open();
            SqlCommand com = new SqlCommand("SELECT *, c.nome as nome_cartorio, a.status as status_atendimento, u.nome as nome_usuario FROM atendimento AS a INNER JOIN usuarios AS u ON u.id = a.id_usuario_cadastro INNER JOIN cartorios AS c ON a.id_cartorio = c.id", con);
            SqlDataReader rdr = com.ExecuteReader();
            while (rdr.Read())
            {
                atendimento ate = new atendimento();
                ate.cartorios = new cartorios();

                ate.id = Convert.ToInt32(rdr["id"]);
                ate.id_cartorio = Convert.ToInt32(rdr["id_cartorio"]);
                ate.cartorios.nome = rdr["nome_cartorio"].ToString();
                ate.titulo = rdr["titulo"].ToString();
                ate.descricao = rdr["descricao"].ToString();
                ate.data_abertura = Convert.ToDateTime(rdr["data_abertura"]);
                if (!rdr.IsDBNull(rdr.GetOrdinal("data_conclusao")))
                {
                    ate.data_conclusao = Convert.ToDateTime(rdr["data_conclusao"]);
                }
                ate.contato = rdr["contato"].ToString();
                ate.origem = rdr["origem"].ToString();
                ate.prioridade = rdr["prioridade"].ToString();
                ate.status = rdr["status_atendimento"].ToString();
                ate.usuarios.nome = rdr["nome_usuario"].ToString();
                ate.id_usuario_cadastro = Convert.ToInt32(rdr["id_usuario_cadastro"]);
                ate.id_usuario_atendimento = Convert.ToInt32(rdr["id_usuario_atendimento"]);

                lst.Add(ate);
            }
            return lst;
        }
    }

The problem is in the following line:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ate.usuarios.nome = rdr["nome_usuario"].ToString();

Class 1:

public partial class atendimento
    {
        public atendimento()
        {
            this.lancamentos = new HashSet<lancamentos>();
        }

        public int id { get; set; }
        public int id_cartorio { get; set; }
        public string titulo { get; set; }
        public string descricao { get; set; }
        public Nullable<System.DateTime> data_abertura { get; set; }
        public Nullable<System.DateTime> data_conclusao { get; set; }
        public string contato { get; set; }
        public string origem { get; set; }
        public string prioridade { get; set; }
        public string status { get; set; }
        public int id_usuario_cadastro { get; set; }
        public int id_usuario_atendimento { get; set; }
        public virtual usuarios usuarios { get; set; }
        public virtual cartorios cartorios { get; set; }
        public virtual usuarios usuarios1 { get; set; }
        public virtual ICollection<lancamentos> lancamentos { get; set; }
    }

Class 2:

public partial class usuarios
    {
        public usuarios()
        {
            this.anexos = new HashSet<anexos>();
            this.atendimento = new HashSet<atendimento>();
            this.atendimento1 = new HashSet<atendimento>();
            this.lancamentos = new HashSet<lancamentos>();
            this.lancamentos1 = new HashSet<lancamentos>();
        }

        public int id { get; set; }
        public string nome { get; set; }
        public string email { get; set; }
        public string senha { get; set; }
        public string status { get; set; }
        public string acesso { get; set; }
        public int id_setor { get; set; }

        public virtual ICollection<anexos> anexos { get; set; }
        public virtual ICollection<atendimento> atendimento { get; set; }
        public virtual ICollection<atendimento> atendimento1 { get; set; }
        public virtual ICollection<lancamentos> lancamentos { get; set; }
        public virtual ICollection<lancamentos> lancamentos1 { get; set; }
        public virtual setor setor { get; set; }
    }

>Solution :

usuarios is not created , try this

 while (rdr.Read())
{
var ate = new atendimento{
cartorios = new cartorios(),
usuarios=new usuarious()
}
....
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading