Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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...
C#
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);
           //MessageBox.Show("El ultimo estudiante anhadido correctamente fue " + gclase.Estudiantes.Last.Value.Nombre);
       }
Posted
Updated 15-Nov-13 2:22am
v2
Comments
Ron Beyer 15-Nov-13 8:02am    
How can we tell you how to modify your code, if you don't give us your code? Its like going to a car shop and asking them to fix your car, without bringing your car. We can't go to your house, see your screen, operate your keyboard or access your hard drive, so you have to post the relevant parts of your code so we can see your problem.
On which line, it throws the exception?
Cesar Bretana Gonzalez 15-Nov-13 10:50am    
var auxGCient = control.GruposCientificos.Find(gcient).Value;
var auxGClase = control.Grupos_Clases.Find(gclase).Value;'
that's return null, I don't known why
Try like below...

if(control.GruposCientificos.Find(gcient))
{
var auxGCient = control.GruposCientificos.Find(gcient).Value;
}
BillWoodruff 15-Nov-13 23:47pm    
I'd suggest you edit your question, clearly state the names of each ComboBox, and the names of the Lists. Show the declaration of your linked lists.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900