3 Replies - 949 Views - Last Post: 23 November 2014 - 10:04 PM Rate Topic: -----

#1 Red Prince   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 03-June 12

Object reference not set to an instance of an object.

Posted 23 November 2014 - 11:35 AM

Hello Everyone, I Was Working On My WPF Project & I'm Stuck In A Problem! I Have Two 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 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";
}



Also, In The Cart Form, I Initilized BindingSouce While Declaration & The Code Of Cart For Its Attribute Is As Follows:
private DataSet dataSet;
private BindingSource bindingSouce = new BindingSource();
private string purchasedItemId = "";



Can Anybody Please Help Me To Get Rid From This Error!
Thanks In Advance....!!


Is This A Good Question/Topic? 0
  • +

Replies To: Object reference not set to an instance of an object.

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6534
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Object reference not set to an instance of an object.

Posted 23 November 2014 - 12:22 PM

This is the very first error that I covered in this tutorial:

What does this error mean and how do I fix it?
Was This Post Helpful? 1
  • +
  • -

#3 Red Prince   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 03-June 12

Re: Object reference not set to an instance of an object.

Posted 23 November 2014 - 08:43 PM

I Read Your Article, But I Already Added This Line In The Cart Class:
this.dataGridView1.DataSource = this.bindingSouce;



So, Why Adding Following Line Raise An Exception:
this.dataGridView1.DataSource = this.bindingSouce;



I Debug My Program But Don't Understand Why This Exception Is Raising..!!
What I Understand Is That This Might Be Passing A Reference Variable [DataSet] To The Cart Form From ViewProduct Form
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6220
  • View blog
  • Posts: 21,469
  • Joined: 05-May 12

Re: Object reference not set to an instance of an object.

Posted 23 November 2014 - 10:04 PM

You didn't read the article close enough. You failed to inspect the values of the variables. If you look closely, I'm guessing that dataGridView1 is null. The reason for this is because with your second constructor, you didn't call InitializeComponent().

I'm confused. You say that you are working on a WPF project, yet you are referencing a dataGridView1. I thought that that there is no DataGridView class in WPF, only in WinForms.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1