0

When I try to check if the Person Email of that Colector is right my program crashes saying that An unhandled exception of type 'System.NullReferenceException' occurred in Aplicativo Windows Phone.dll. Why? The error is happening on the

   if (Coletor.Pessoa.Email == email)
   {
       Coletores.Add(Coletor);
   }

My Coletor class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq.Mapping;
using System.Data.Linq;
using Microsoft.Phone.Data.Linq;
using Microsoft.Phone.Data.Linq.Mapping;
using System.ComponentModel;

namespace Aplicativo_Windows_Phone
{
#pragma warning disable 0169, 0649
    [Table(Name = "Coletores")]
    public class Coletor : INotifyPropertyChanged
    {
        private int _id;
        [Column(IsPrimaryKey = true, IsDbGenerated = true)]
        public int Id { get { return _id; }
            set { _id = value;
            OnPropertyChanged("Id");
            }
        }

        private float _lat;
        [Column]
        public float Latitude { get { return _lat; }
            set { _lat = value;
            OnPropertyChanged("Latitude");
            }
        }

        private float _long;
        [Column]
        public float Longitude { get { return _long; }
            set { _long = value;
            OnPropertyChanged("Longitude");
            }
        }

        [Column(Name = "Pessoa")]
        private int? pessoaId;
        private EntityRef<Pessoa> _pessoa = new EntityRef<Pessoa>();
        [Association(Name = "FK_Coletores_ColetorPessoas", IsForeignKey = true, Storage = "_pessoa", ThisKey = "pessoaId")]
        public Pessoa Pessoa
        {
            get { return _pessoa.Entity; }
            set { _pessoa.Entity = value; }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}

The code that I am trying to check the person email:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (NavigationContext.QueryString.TryGetValue("email", out email))
            {
                List<HyperlinkButton> listaLinks = new List<HyperlinkButton>();

                AppDataContext db = new AppDataContext();

                List<Coletor> Coletores = new List<Coletor>();

                foreach (var Coletor in db.Coletores)
                {
                    if (Coletor.Pessoa.Email == email)
                    {
                        Coletores.Add(Coletor);
                    }
                }
                int i = 0;
                foreach( Coletor coletor in Coletores)
                {
                    HyperlinkButton aux = new HyperlinkButton();
                    aux.Content = "Coletor " + (i + 1).ToString();
                    aux.FontSize = 24;
                    aux.NavigateUri = new Uri("/OcorrenciasPage.xaml?coletorId=" + coletor.Id.ToString(), UriKind.RelativeOrAbsolute);
                    listaLinks.Add(aux);
                    i++;
                }

                ListBox coletores = new ListBox();
                coletores.ItemsSource = listaLinks;
                stcList.Children.Add(coletores);
            }
            base.OnNavigatedTo(e);
        }

I think the error is in my class, that I did something wrong on the relationship with the other class.

Ps. My Coletor belongs to just one Pessoa, but one Pessoa can have many Coletors.

Gustavo Mendonça
  • 1,925
  • 3
  • 15
  • 26
  • Where is `aux` defined? And does every `Coletor` have exactly one `Pessoa` or at most one? – Guvante Mar 17 '15 at 16:13
  • Do you have the stack trace from the exception? That will help narrow down where the error is occuring – amura.cxg Mar 17 '15 at 16:13
  • @Guvante Need to have exactly one `Pessoa`. – Gustavo Mendonça Mar 17 '15 at 16:14
  • @amura.cxg The error only shows that have an unhandled exception. – Gustavo Mendonça Mar 17 '15 at 16:16
  • Without a stack trace that shows at least which function caused the error this is difficult to track down. stcList is not defined here, and it is hard to know if its Children property is always non-null. – Guvante Mar 17 '15 at 17:22
  • @Guvante I am getting error on my `if(Coletor.Pessoa.Email == email)` and my stcList is defined on my xaml file. I am sure that the error is not in my stcList because I´ve made tests and its all ok with this. – Gustavo Mendonça Mar 17 '15 at 17:31
  • @GustavoMendonça: Then you probably have a `Coletor` without a matching `Pessoa`, causing the `.Email` to dereference null. – Guvante Mar 17 '15 at 18:11

0 Answers0