Khizar Iqbal

Khizar Iqbal

  • NA
  • 44
  • 9k

Object reference not set to an instance of an object.

Nov 22 2014 2:42 PM
Hello Everyone, I Was Working On My WPF Project & I'm Stuck In A Problem! I Have To Form Names ViewProduct & Cart; I'm Creating An Instance Of Cart In The ViewProduct Form & Passing A DataSet Set To The Cart Class [Whose Constructor Is Overloaded & That Accept A DataSet]
Here Is My Code Of ViewProduct Form:
 private DBConnect connection = new DBConnect(); private DataSet dataSet = new DataSet();
private BindingSource bindingSource = new BindingSource();
private Cart cart = null; 
.................................... Some Other Stuff ...................................... 

Function In Which Cart Instance Is Created! 
private void addCartBtn_Click(object sender, EventArgs e) {
this.cart = new Cart(this.dataSet);
this.cart.Show();
foreach(DataGridViewRow row in this.dataGridView1.SelectedRows)
this.cart.AddProcudt(int.Parse(row.Cells["id"].Value.ToString()));
}
..................................... 

Cart Constructor:
public Cart(DataSet aDataSet) {
this.dataSet = aDataSet;
this.bindingSouce.DataSource = this.dataSet.Tables["Product"];
this.dataGridView1.DataSource = this.bindingSouce;
bindingSouce.Filter = "id=0";


But I Get Error On The Following Line:
this.dataGridView1.DataSource = this.bindingSouce;
And The Error Is:
An unhandled exception of type 'System.NullReferenceException' occurred in ePurchase.exe
Additional information: Object reference not set to an instance of an object.
use the new keyword to create the instance of the object
On Calling Default Cart Constructor I Get No Error Because I Fill The dataset By Creating An Object Of DBConnect Class & Calling  It's Method; Doing This I Get No Error But Calling 2nd Constructor I Get The Above Error!
Default Cart Constructor
public Cart()
{
InitializeComponent();
this.dataGridView1.AutoGenerateColumns = false;
DBConnect con = new DBConnect();
this.dataSet = con.GetRecord("Product");
this.bindingSouce.DataSource = this.dataSet.Tables["Product"];
this.dataGridView1.DataSource = this.bindingSouce;
bindingSouce.Filter = "id=0";


Can Anybody Please Help Me To Get Rid From This Error!

Thanks In Advance....!! 


Answers (4)