Reputation: 949
This is my code:
private void button1_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
Pen p = new Pen(Brushes.Blue);
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
float p1x = float.Parse(dr.Cells["p1x"].Value.ToString());
float p1y = float.Parse(dr.Cells["p1y"].Value.ToString());
float p2x = float.Parse(dr.Cells["p2x"].Value.ToString());
float p2y = float.Parse(dr.Cells["p2y"].Value.ToString());
g.DrawEllipse(p, p1x, p1y, 10, 10);
g.DrawEllipse(p, p2x, p2y, 10, 10);
g.FillEllipse(Brushes.Black, p1x, p1y, 10, 10);
g.FillEllipse(Brushes.Black, p2x, p2y, 10, 10);
g.DrawLine(p, p1x, p1y, p2x, p2y);
}
}
On executing the above code, I'm getting the runtime exception "Object reference not set to an instance of an object." Please help.
Upvotes: 0
Views: 1001
Reputation: 4464
More than likely one of your cells has a null value i.e.
dr.Cells["p1x"].Value == null
You can't use ToString()
on null
so you get that error.
Upvotes: 1