if(atributo != null)
usually resolves, but this pollutes the code, "Ah, but this variable must have a value right?" the problem happens just when I assign a value while filling in the attributes of an object, this error appears.
"An exception of type 'System.NullReferenceException' occurred in ProjectName.dll but was not handled in user code" *
I have an associative class that saves Permissoes
of a page and the user that has this permission, while I try to fill the associative ( FluxoUsuario
) it points the error:
Here is the excerpt from FluxosController.cs
:
for (int i = 0; i < vwfluxo.Usuarios.Count(); i++)
{
if (Request.Params["cb" + i] != null)
{
FluxoUsuario fu = new FluxoUsuario();
var a = bool.Parse(Request.Form["cb" + i].Split(',')[0]);
var b = Request.Params["rb" + i]; //Pega valor do radioButton
if (a)//Verifica se a checkBox está marcada
{
if(db.FluxoUsuario.ToList().Where(x => x.Usuario.Equals(vwfluxo.Usuarios[i])).Count() > 0 == true)
{
}
fu.Fluxo.FluxoID = fluxo.FluxoID; //O Erro acontece nessa linha.
//fu.Fluxo é do tipo Fluxo, FluxoID é int. fu.Fluxo.FluxoID realmente precisa estar null quando acontece o erro, afinal, está sendo atribuido um valor para ele nesse momento.
fu.Usuario.IDUser = vwfluxo.Usuarios[i].IDUser;
fu.Fluxo = fluxo;
fu.Usuario = vwfluxo.Usuarios[i];
if (b == "ler")
fu.TipoPermissao = TipoPermissao.Ler;
else
fu.TipoPermissao = TipoPermissao.LerEscrever;
if (!fluxo.UsuariosPermitidos.Contains(fu))
{
fluxo.UsuariosPermitidos.Add(fu);
}
}
}
}