Hello, in my project, I have a combobox, and a lists of items, my problem is when i going to add an object, if my object doesn't exists in my list of objects, i want to add to my list and to add a new item to the combobox, but if there exists, then I find it and add the object to the List, then here is the problem, my code doesn't find the object that i want in my list, when i'm trying to add to that object, my code throws a NullReferenceException, how can I solve this problem...???
Here is my code, the real problem is:
I got two LinkedList, one of ScientificGroup and other of ClassRooms, the contains on each of their node LinkedLists of Students, the I want to add in my Visual to a combobox the two groups if they doesn't exists, but if one of them exists, i want to find it in my List and add the student to that group, my code is in spanish, my english is no so good, so excuse any issues, thanks...
here my control class is CEIS, its name in my code is control...
private void btnAddEst_Click(object sender, EventArgs e)
{
gcient = new GrupoCientifico(tbGrupoCientifico.Text);
gclase = new GrupoClase(tbGrupoClase.Text);
est = new Estudiante(tbNombre.Text, tbId.Text, tbGrupoCientifico.Text, tbGrupoClase.Text);
if (!cbGCient.Items.Contains(gcient.TemaInvestigacion) && !cbGCient.Items.Contains(gclase.Identificador))
{
control.AddGrupoCientifico(gcient);
cbGCient.Items.Add(gcient.TemaInvestigacion.ToString());
control.GruposCientificos.Last.Value.AddParticipante(est);
control.AddGrupoClase(gclase);
cbGCl.Items.Add(gclase.Identificador);
control.Grupos_Clases.Last.Value.AddEstudiantes(est);
}
else if (!cbGCient.Items.Contains(gcient.TemaInvestigacion) && cbGCient.Items.Contains(gclase.Identificador))
{
control.AddGrupoCientifico(gcient);
cbGCient.Items.Add(gcient.TemaInvestigacion.ToString());
control.GruposCientificos.Last.Value.AddParticipante(est);
control.Grupos_Clases.Find(gclase).Value.Estudiantes.AddLast(est);
}
else if (cbGCient.Items.Contains(gcient.TemaInvestigacion) && !cbGCient.Items.Contains(gclase.Identificador))
{
control.AddGrupoClase(gclase);
cbGCl.Items.Add(gclase.Identificador);
control.Grupos_Clases.Last.Value.AddEstudiantes(est);
control.GruposCientificos.Find(gcient).Value.AddParticipante(est);
}
else
{
var auxGCient = control.GruposCientificos.Find(gcient).Value;
var auxGClase = control.Grupos_Clases.Find(gclase).Value;
try
{
auxGCient.AddParticipante(est);
}
catch (NullReferenceException nre)
{
MessageBox.Show(nre.Message);
}
try
{
auxGClase.AddEstudiantes(est);
}
catch (NullReferenceException nre2)
{
MessageBox.Show(nre2.Message);
}
}
tbNombre.Clear();
tbId.Clear();
tbGrupoClase.Clear();
tbGrupoCientifico.Clear();
MessageBox.Show("Estudiante añadido correctamente", "Informacion", MessageBoxButtons.OK);
}