How to resolve error C# "Nullreferenceexception: Object Reference not set to an instance of an Object."?

Asked

Viewed 822 times

0

I’m trying to finish a project only that is showing error on the foreach part, and in VSCODE does not report any problem. I’m learning C# now and I can’t figure it out on my own.

this is homecontroller:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Projeto_PetVet.Models;

namespace Projeto_PetVet.Controllers
{
public class HomeController : Controller
{
    private readonly ILogger<HomeController> _logger;

    public HomeController(ILogger<HomeController> logger)
    {
        _logger = logger;
    }

    public IActionResult Index()
    {
        return View();
    }

    public IActionResult Servicos()
    {
        return View();
    }


    public IActionResult PreAgendamento()
    {
        return View();
    }
    
    [HttpPost]
    public IActionResult PreAgendamento(DadosConsulta consulta)
    {
        Consulta.addDados(consulta);
        return View("olhaDados");
    }

    public IActionResult Sucesso()
    {
        return View();
    }

    public IActionResult olhaDados() {
    List<DadosConsulta> consulta = Consulta.olhaDados();
    return View(consulta);
    }


    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error()
    {
        return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }
}
}

These are my Models: Query.Cs:

using System.Collections.Generic;

namespace Projeto_PetVet.Models
{
public class Consulta
{
    static List<DadosConsulta> infos = new List<DadosConsulta>();

    public static void addDados(DadosConsulta consulta) {
        infos.Add(consulta);
    }

    public static List<DadosConsulta> olhaDados() {
        return infos;
    }
}
}

Dadosconsulta.Cs

namespace Projeto_PetVet.Models
{
public class DadosConsulta
{
    public string Nome {get; set;}
    public string Animal {get; set;}
    public string Data {get; set;}
    public string Telefone {get; set;}
    public string Horario {get; set;}
    public string Necessidade {get; set;}
}
}

And this is my View, where the bug is showing up in the foreach:

@model List<DadosConsulta>
@{
ViewData["Title"] = "Pré agendamento";
}
<h1>Confirme suas informações</h1>

<p>Confirme seu pré agendamento.</p>
<p>Todos os dados estão corretos?</p>

<table class="table table-bordered">
<tr>
    <td>Nome</td>
    <td>Telefone</td>
    <td>Data</td>
    <td>Horário</td>
    <td>Animal</td>
    <td>Necessidade</td>
</tr>


@{
    Consulta infos = new Consulta();

if (Model != null) { foreach (Dadosconsulta consulta in Model) { @query. Name @query. Telephone @query. Date @query. Time @query. Animal @query. Need } } }

<a asp-action="Sucesso">Tudo certo!</a>
<a asp-action="PreAgendamento">Corrigir dados</a>

1 answer

0

//It is necessary to fill the bojetos before the foreach

@model list @{ Viewdata["Title"] = "Pre-scheduling"; }

Confirm your information

Confirm your pre-schedule.

All the data is correct?

Name Telephone Date Schedule Animal Need

@{ Query Infos = new Query();

    infos.Nome = "Jose";
    infos.Telefone = "(011) 9-8623-6091";
    infos.Data = "10/10/2020";
    infos.Horario = "14:30";
    infos.Animal = "Macaco";
    infos.Necessidade = "Preencher objetos";


foreach (DadosConsulta consulta in Model)
{
    <tr>
        <td>@consulta.Nome</td>
        <td>@consulta.Telefone</td>
        <td>@consulta.Data</td>
        <td>@consulta.Horario</td>
        <td>@consulta.Animal</td>
        <td>@consulta.Necessidade</td>
    </tr>
}

}

All right! Correct data

  • 1

    You have to fill the values of the objects before entering the foreach. Right and fill the values in the backend searching in the database

  • I have not yet left for the database :( But I will fix the code again, thank you for the tips

Browser other questions tagged

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