I have two subclasses of animal (dog which has a last walk date as extra value and cat which has a bad habbit as a extra value) in the code below i create either a cat or a dog
public partial class AdministrationForm : Form
{
private Animal animal;
private Administration admin;
public AdministrationForm()
{
InitializeComponent();
animalTypeComboBox.SelectedIndex = 0;
animal = null;
}
private void createAnimalButton_Click(object sender, EventArgs e)
{
if(animalTypeComboBox.SelectedItem == "Dog")
{
SimpleDate dateBirth = new SimpleDate(ttbdateBirth.Value.Day,
ttbdateBirth.Value.Month,
ttbdateBirth.Value.Year);
SimpleDate dateLast1 = new SimpleDate(dateLast.Value.Day, dateLast.Value.Month, dateLast.Value.Year);
Animal animal = new Dog(Convert.ToString(tbId.Text), dateBirth, tbName.Text, dateLast1);
admin.Add(animal);
}
if (animalTypeComboBox.SelectedItem == "Cat")
{
SimpleDate dateBirth = new SimpleDate(ttbdateBirth.Value.Day, ttbdateBirth.Value.Month, ttbdateBirth.Value.Year);
Animal animal = new Cat(Convert.ToString(tbId.Text), dateBirth, tbName.Text, tbBad.Text);
admin.Add(animal);
}
}
}
After this is created I send this to an administration class which is suposed to add this dog or cat to an animal list however as mentioned in the title I get the NullReference error. The class that is supposed to add the cat or dog is as follows:
class Administration
{
private List<Animal> animals;
public List<Animal> Animals { get { return new List<Animal>(animals); } }
public Administration ()
{
animals = new List<Animal>();
}
public bool Add(Animal animal)
{
foreach (Animal dog in animals)
{
if(animal.ChipRegistrationNumber == dog.ChipRegistrationNumber)
{
MessageBox.Show("dog already added");
break;
}
}
foreach (Animal cat in animals)
{
if(animal.ChipRegistrationNumber == cat.ChipRegistrationNumber)
{
MessageBox.Show("dog already added");
break;
}
}
return true;
}
}
Now my question is what am I doing wrong and how could I fix this so that I am able to add a cat or dog to the list.