null ref exception after adding object to array

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hello all
I have exception which really doesnt make sense to me.this is my code

foreach (DataRow drowAMC in DBinter.dtabActivemenuContent.Rows)
{
if(x!=byte.Parse(drowAMC[2].ToString()))
{
this.lview = new System.Windows.Forms.ListView();
lview.CheckBoxes = true;
lview.Location = new System.Drawing.Point(8, 8);
lview.Size = new System.Drawing.Size(216, 168);
lview.View = System.Windows.Forms.View.List;


System.Windows.Forms.TabPage tpage = new System.Windows.Forms.TabPage();
tpage.Location = new System.Drawing.Point(4, 4);
tpage.Size = new System.Drawing.Size(232, 184);
tpage.Text = drowAMC[3].ToString();

tpage.Controls.Add(lview);
this.tabFormOrder.Controls.Add(tpage);

x=byte.Parse(drowAMC[2].ToString());
}

this.lViewSubItm = new
System.Windows.Forms.ListViewItem.ListViewSubItem();
this.lViewSubItm.Text = drowAMC[4].ToString();


this.lViewItm = new System.Windows.Forms.ListViewItem();
this.lViewItm.Text=drowAMC[0].ToString();
this.lViewItm.Checked=bool.Parse(drowAMC[1].ToString());


this.lViewItm.SubItems.Add(this.lViewSubItm);
this.lview.Items.Add(this.lViewItm);

OrderContent OCnt=new
OrderContent((int)drowAMC[4],1,this.MenuID,this.PatientCode,drowAMC[0].ToString(),this.lViewItm.Checked);
this.httest.Add(OCnt);

if (this.lViewItm.Checked)
{

// this.Order.OrderContent.Add(drowAMC[4].ToString(),OCnt);
// this.lboxOrderSummary.Items.Add(lViewItm.Text);
}

}
this.lboxOrderSummary.DataSource=this.httest;
this.lboxOrderSummary.DisplayMember="ItemDesc";
DBinter=null;
}

the code works fine if i comment out the line "this.httest.Add(OCnt);"
if i activate it then the line next to it " if (this.lViewItm.Checked)"
generate a exception null ref.

it seems the app loose all ref. i cannot understnad why

can any one help please

thanks in advance for the time
 
Don't know exactly, but have you deeply debugged the application?

Before these two lines:

this.lViewItm.SubItems.Add(this.lViewSubItm);
this.lview.Items.Add(this.lViewItm);

what value do you have in the previous line of code, exactly in
"this.lViewItm.Checked"? true or false?

The value should continue there. Could it be that you are adding a null
value in "this.httest.Add(OCnt); "?

Are you working with VS.NET 2005 Beta2? maybe a bug?

Regards.


Is OCnt != null?
 
hello Lonifasiko

I atep through each command. i really want to create the Ocnt and add it to
the array only if list view item check is true. i had the two command inside
the if satatement. once if becomes true i had the had the null ref exception.
so i brought it out to check. still the null exception happens. i knwo which
command is the problem. its the array add method. once i comment it out it
works fine. it all doesnt make any logical sence. becouse i do the same that
is adding object to array in a for each loop in other places. and it works. i
have really run out of ideas as to why it happens. i use vs 2003 cf 1.1 with
sp3

thanks
 
Since OCnt was initiated in the previous line, and drowAMC[4].ToString()
was already used either. I can assume that either Order or OrderContent
is null. You have to check its values in debuger.
 
the drowAMC[4]. is still preserved. the object ocnt is also stil intact

Sergey Bogdanov said:
Since OCnt was initiated in the previous line, and drowAMC[4].ToString()
was already used either. I can assume that either Order or OrderContent
is null. You have to check its values in debuger.

--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


chamal said:
hello all
I have exception which really doesnt make sense to me.this is my code

foreach (DataRow drowAMC in DBinter.dtabActivemenuContent.Rows)
{
if(x!=byte.Parse(drowAMC[2].ToString()))
{
this.lview = new System.Windows.Forms.ListView();
lview.CheckBoxes = true;
lview.Location = new System.Drawing.Point(8, 8);
lview.Size = new System.Drawing.Size(216, 168);
lview.View = System.Windows.Forms.View.List;


System.Windows.Forms.TabPage tpage = new System.Windows.Forms.TabPage();
tpage.Location = new System.Drawing.Point(4, 4);
tpage.Size = new System.Drawing.Size(232, 184);
tpage.Text = drowAMC[3].ToString();

tpage.Controls.Add(lview);
this.tabFormOrder.Controls.Add(tpage);

x=byte.Parse(drowAMC[2].ToString());
}

this.lViewSubItm = new
System.Windows.Forms.ListViewItem.ListViewSubItem();
this.lViewSubItm.Text = drowAMC[4].ToString();


this.lViewItm = new System.Windows.Forms.ListViewItem();
this.lViewItm.Text=drowAMC[0].ToString();
this.lViewItm.Checked=bool.Parse(drowAMC[1].ToString());


this.lViewItm.SubItems.Add(this.lViewSubItm);
this.lview.Items.Add(this.lViewItm);

OrderContent OCnt=new
OrderContent((int)drowAMC[4],1,this.MenuID,this.PatientCode,drowAMC[0].ToString(),this.lViewItm.Checked);
this.httest.Add(OCnt);

if (this.lViewItm.Checked)
{

// this.Order.OrderContent.Add(drowAMC[4].ToString(),OCnt);
// this.lboxOrderSummary.Items.Add(lViewItm.Text);
}

}
this.lboxOrderSummary.DataSource=this.httest;
this.lboxOrderSummary.DisplayMember="ItemDesc";
DBinter=null;
}

the code works fine if i comment out the line "this.httest.Add(OCnt);"
if i activate it then the line next to it " if (this.lViewItm.Checked)"
generate a exception null ref.

it seems the app loose all ref. i cannot understnad why

can any one help please

thanks in advance for the time
 
Please, read carefully what I wrote in my previous post. You have to
check that:

a) this.Order is not null
b) this.Order.OrderContent is not null.


--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


chamal said:
the drowAMC[4]. is still preserved. the object ocnt is also stil intact

:

Since OCnt was initiated in the previous line, and drowAMC[4].ToString()
was already used either. I can assume that either Order or OrderContent
is null. You have to check its values in debuger.

--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


chamal said:
hello all
I have exception which really doesnt make sense to me.this is my code

foreach (DataRow drowAMC in DBinter.dtabActivemenuContent.Rows)
{
if(x!=byte.Parse(drowAMC[2].ToString()))
{
this.lview = new System.Windows.Forms.ListView();
lview.CheckBoxes = true;
lview.Location = new System.Drawing.Point(8, 8);
lview.Size = new System.Drawing.Size(216, 168);
lview.View = System.Windows.Forms.View.List;


System.Windows.Forms.TabPage tpage = new System.Windows.Forms.TabPage();
tpage.Location = new System.Drawing.Point(4, 4);
tpage.Size = new System.Drawing.Size(232, 184);
tpage.Text = drowAMC[3].ToString();

tpage.Controls.Add(lview);
this.tabFormOrder.Controls.Add(tpage);

x=byte.Parse(drowAMC[2].ToString());
}

this.lViewSubItm = new
System.Windows.Forms.ListViewItem.ListViewSubItem();
this.lViewSubItm.Text = drowAMC[4].ToString();


this.lViewItm = new System.Windows.Forms.ListViewItem();
this.lViewItm.Text=drowAMC[0].ToString();
this.lViewItm.Checked=bool.Parse(drowAMC[1].ToString());


this.lViewItm.SubItems.Add(this.lViewSubItm);
this.lview.Items.Add(this.lViewItm);

OrderContent OCnt=new
OrderContent((int)drowAMC[4],1,this.MenuID,this.PatientCode,drowAMC[0].ToString(),this.lViewItm.Checked);
this.httest.Add(OCnt);

if (this.lViewItm.Checked)
{

// this.Order.OrderContent.Add(drowAMC[4].ToString(),OCnt);
// this.lboxOrderSummary.Items.Add(lViewItm.Text);
}

}
this.lboxOrderSummary.DataSource=this.httest;
this.lboxOrderSummary.DisplayMember="ItemDesc";
DBinter=null;
}

the code works fine if i comment out the line "this.httest.Add(OCnt);"
if i activate it then the line next to it " if (this.lViewItm.Checked)"
generate a exception null ref.

it seems the app loose all ref. i cannot understnad why

can any one help please

thanks in advance for the time
 
thanks sergey

i resolve the problem thak you for your time

cheers

Sergey Bogdanov said:
Please, read carefully what I wrote in my previous post. You have to
check that:

a) this.Order is not null
b) this.Order.OrderContent is not null.


--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


chamal said:
the drowAMC[4]. is still preserved. the object ocnt is also stil intact

:

Since OCnt was initiated in the previous line, and drowAMC[4].ToString()
was already used either. I can assume that either Order or OrderContent
is null. You have to check its values in debuger.

--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


chamal kalamulla wrote:

hello all
I have exception which really doesnt make sense to me.this is my code

foreach (DataRow drowAMC in DBinter.dtabActivemenuContent.Rows)
{
if(x!=byte.Parse(drowAMC[2].ToString()))
{
this.lview = new System.Windows.Forms.ListView();
lview.CheckBoxes = true;
lview.Location = new System.Drawing.Point(8, 8);
lview.Size = new System.Drawing.Size(216, 168);
lview.View = System.Windows.Forms.View.List;


System.Windows.Forms.TabPage tpage = new System.Windows.Forms.TabPage();
tpage.Location = new System.Drawing.Point(4, 4);
tpage.Size = new System.Drawing.Size(232, 184);
tpage.Text = drowAMC[3].ToString();

tpage.Controls.Add(lview);
this.tabFormOrder.Controls.Add(tpage);

x=byte.Parse(drowAMC[2].ToString());
}

this.lViewSubItm = new
System.Windows.Forms.ListViewItem.ListViewSubItem();
this.lViewSubItm.Text = drowAMC[4].ToString();


this.lViewItm = new System.Windows.Forms.ListViewItem();
this.lViewItm.Text=drowAMC[0].ToString();
this.lViewItm.Checked=bool.Parse(drowAMC[1].ToString());


this.lViewItm.SubItems.Add(this.lViewSubItm);
this.lview.Items.Add(this.lViewItm);

OrderContent OCnt=new
OrderContent((int)drowAMC[4],1,this.MenuID,this.PatientCode,drowAMC[0].ToString(),this.lViewItm.Checked);
this.httest.Add(OCnt);

if (this.lViewItm.Checked)
{

// this.Order.OrderContent.Add(drowAMC[4].ToString(),OCnt);
// this.lboxOrderSummary.Items.Add(lViewItm.Text);
}

}
this.lboxOrderSummary.DataSource=this.httest;
this.lboxOrderSummary.DisplayMember="ItemDesc";
DBinter=null;
}

the code works fine if i comment out the line "this.httest.Add(OCnt);"
if i activate it then the line next to it " if (this.lViewItm.Checked)"
generate a exception null ref.

it seems the app loose all ref. i cannot understnad why

can any one help please

thanks in advance for the time
 
Back
Top