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:
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?