0

I'm developing a .net Core web-app, using a PostgreSQL v9.4 database, on my Windows laptop. The web-app is the deployed on a Debian server, using the same .net Core SDK and database settings.

I defined a model Utente as:

public class Utente : IdentityUser<int>
{
    [Display(Name = "Nome")]
    public string Nome { get; set; }

    [Display(Name = "Creato il")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime Creato { get; set; }

    [Display(Name = "Attivato il")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime? Attivato { get; set; }

    [Required]
    [Display(Name = "Creato da")]
    public int? CreatoDaId { get; set; }
    public Utente CreatoDa { get; set; }

    [InverseProperty("CreatoDa")]
    public virtual List<Utente> UtentiCreati { get; set; }
    [InverseProperty("Utente")]
    public virtual List<Invito> InvitiRicevuti { get; set; }
    [InverseProperty("CreatoDa")]
    public virtual List<Invito> InvitiCreati { get; set; }        
    public virtual List<Luogo> LuoghiCreati { get; set; }
    [InverseProperty("CreatoDa")]
    public virtual List<Assegnazione> AssegnazioniCreate { get; set; }
    [InverseProperty("Utente")]
    public virtual List<Assegnazione> Assegnazioni { get; set; }
    [InverseProperty("CreatoDa")]
    public virtual List<Abbonamento> AbbonamentiCreati { get; set; }
    [InverseProperty("Utente")]
    public virtual List<Abbonamento> AbbonamentiSottoscritti { get; set; }
    public virtual List<Accesso> Accessi { get; set; }        
}

and then I'm querying my db as follows:

var utente = await _db.Users
                      .Include(u => u.Roles)
                      .Include(u => u.CreatoDa)
                      .Include(u => u.InvitiRicevuti)
                      .Include(u => u.InvitiCreati)
                      .Include(u => u.UtentiCreati)
                      .Include(u => u.Assegnazioni)
                      .Include(u => u.AssegnazioniCreate)
                      .Include(u => u.LuoghiCreati)
                      .Include(u => u.AbbonamentiSottoscritti).ThenInclude(x => x.AbbonamentiAccessi).ThenInclude(y => y.Accesso).ThenInclude(z => z.Modulo)
                      .Include(u => u.AbbonamentiCreati).ThenInclude(x => x.AbbonamentiAccessi).ThenInclude(y => y.Accesso).ThenInclude(z => z.Modulo)
                      .SingleOrDefaultAsync(u => u.Id == id);

and then I get the data in the list with a foreach:

if (utente.AssegnazioniCreate != null && utente.AssegnazioniCreate.Count > 0)
    foreach (var assegnazione in utente.AssegnazioniCreate.AsQueryable().Include(x => x.Luogo).Include(x => x.Utente))
    {
        model.AssegnazioniCreate.Add(new DatiAssegnazioneCreata
        {
            UtenteAssegnato = assegnazione.Utente.Nome ?? assegnazione.Utente.Email,
            Nome = assegnazione.Luogo != null ? assegnazione.Luogo.Nome : "si è verificato un errore",
            Indirizzo = assegnazione.Luogo != null ? assegnazione.Luogo.Indirizzo : "si è verificato un errore",
            AssegnatoIl = assegnazione.CreatoIl
        });
    }

Even though I'm explicitely checking for null values of the list, the foreach loop start and get a NullReference Exception when trying to load the first element, nonetheless. I tried to use a for loop instead of the foreach:

if (utente.AssegnazioniCreate != null)
{
    var count = utente.AssegnazioniCreate.Count;
    for (int idx = 0; idx < count; idx++)
    {
        var assegnazione = utente.AssegnazioniCreate[idx];
        model.AssegnazioniCreate.Add(new DatiAssegnazioneCreata
        {
             UtenteAssegnato = assegnazione.Utente.Nome ?? assegnazione.Utente.Email,
             Nome = assegnazione.Luogo != null ? assegnazione.Luogo.Nome : "si    verificato un errore",
             Indirizzo = assegnazione.Luogo != null ? assegnazione.Luogo.Indirizzo : "si    verificato un errore",
             AssegnatoIl = assegnazione.CreatoIl
        });
    }
}

but I'm still getting

An unhandled exception occurred while processing the request.
    NullReferenceException: Object reference not set to an instance of an object.

on for (int idx = 0; idx < count; idx++). Moreover, I'm couldn't reproduce this exception on locally: everything works just fine on my laptop.

Every bit of help you could give me will be appreciated so much.

I'm attaching the image of the exception page: exception

UPDATE: after encapsulating every foreach within a try-catch, I managed to avoid the exception. Since it even told me that there was a NullReferenceException in the following:

DatiAbbonamentoCreato abbonamentoCreato = new DatiAbbonamentoCreato

could those errors actually, in some way, be related to the entity framework?

GoGoLander
  • 317
  • 3
  • 11
  • I assume the exception doesn´t occur on the `for`-line (that´s simply not posible), but onle line before, so either is `utente` or `utente.AssegnazioniCreate` null. – HimBromBeere Sep 20 '17 at 11:51
  • According to the error page, It does! Happy to see that this makes no sense to someone else, too – GoGoLander Sep 20 '17 at 11:52
  • Did you assume the line base on the line number, from the release build? If so, the release mode gets optimized and the line numbers arent exactly those that you see in debug. – Rand Random Sep 20 '17 at 11:55
  • I thought about that, so I checked that every list is not null when I'm reading from them. This still is the one that, at this time, is giving me this problem – GoGoLander Sep 20 '17 at 11:57
  • can't you simply add breakpoint and run the code? – balexandre Sep 20 '17 at 12:39
  • The problem is not reproducible on the local machine: it only happens on the remote server. Locally, everything works just fine. Is there a remote debugger for a debian server and asp net core? – GoGoLander Sep 20 '17 at 12:41
  • I think the query was too complex for Entity Framework Core to handle. I broke it down to multiple queries and the problem seems to be solved. Thank you all for the suggestions. – GoGoLander Sep 21 '17 at 10:55

0 Answers0