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.