I cannot figure out why I am getting this error. I am setting an instance to the object I am trying to create. Any help would be really appreciated. I will post my form code and then the class code below that. The application runs fine, it just gives me that null reference error when I click on btnAdd
.
public partial class frmProperties : Form
{
Agent curAgent;
PropertyCollection pc;
int currRecord;
public frmProperties()
{
InitializeComponent();
}
public frmProperties(Agent ac, PropertyCollection pcPassed)
{
InitializeComponent();
curAgent = ac;
pc = pcPassed;
}
//check if there is a property in the list
private void frmProperties_Load(object sender, EventArgs e)
{
if (curAgent.AgentPropertyList.Count > 0)
ShowAll();
}
private void btnNext_Click(object sender, EventArgs e)
{
if (currRecord < curAgent.AgentPropertyList.Count - 1)
{
currRecord++;
ShowAll();
}
else MessageBox.Show("No more properties to view");
}
void ShowAll()
{
txtId.Text = curAgent.AgentPropertyList[currRecord].ToString();
Property p = pc.FindProperty(curAgent.AgentPropertyList[currRecord]);
}
private void btnShowPrev_Click(object sender, EventArgs e)
{
if (currRecord > 0)
{
currRecord--;
ShowAll();
}
else MessageBox.Show("No more properties to view");
}
private void btnAdd_Click(object sender, EventArgs e)
{
pc.AddProperty(Convert.ToInt32(txtId.Text), txtAddress.Text, Convert.ToInt32(txtBedrooms.Text), txtType.Text, Convert.ToInt32(txtSqFt.Text), Convert.ToDouble(txtPrice.Text), txtAgent.Text);
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
Here is the code to the class that the add function was created in:
public class PropertyCollection
{
// list of properties
List<Property> propertyList = new List<Property>();
public List<Property> PropertyList
{
get { return propertyList; }
set { propertyList = value; }
}
public void AddProperty(int id, string address, int bedrooms, string type, int sqft, double price,string agent)
{
Property p = new Property(id,address,bedrooms,type,sqft,price,agent);
propertyList.Add(p);
}
public void RemoveProperty(int id)
{
Property rem = new Property(id);
propertyList.Remove(rem);
}
//loop through and find equivalent
public Property FindProperty(int id)
{
Property find = new Property(id);
for (int i = 0; i < propertyList.Count; i++)
if (propertyList[i].Equals(find))
return propertyList[i];
return null;
}
//Count property and INDEXER
public int Count
{
get { return propertyList.Count; }
}
public Property this[int i]
{
get { return propertyList[i]; }
set { propertyList[i] = value; }
}
}