[C#] System.NullReferenceException

  • Thread starter Martin Høst Normark
  • Start date
M

Martin Høst Normark

Hi everyone

I get a System.NullReferenceException performing this code:

public void Item_Bound(object sender, DataListItemEventArgs e)
{
if((int)(e.Item.DataItem as DataRowView).Row[11] != 0)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
int aktuelt_a_id = (int)(e.Item.DataItem as DataRowView).Row[11];

if(aktuelt_a_id != temp_a_id)
{
temp_a_id = aktuelt_a_id;
Literal myL = (Literal)e.Item.FindControl("myLiteral");
myL.Text = "<h2>" + aktuelt_a_id + "</h2>";
}
}
}
else
;

}

Even this little opertion throws a InvalidCastExcepion

Double Price =
Convert.ToDouble(((DataRowView)(e.Item.DataItem)).Row.ItemArray[3].ToString());


What might be wrong?
 
K

Kevin Spencer

Hi Martin,

Whichever line threw the exception is referencing an object that is null,
has not been instantiated, and/or does not yet exist. I could only guess
which object it is, as you didn't say which line it is. There are quite a
few object references in your code.

Chances are, the invalid cast exception is related to the null reference.
When you try to cast null as anything, it will throw that exception.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
M

Martin Høst Normark

Kevin Spencer said:
Hi Martin,

Whichever line threw the exception is referencing an object that is null,
has not been instantiated, and/or does not yet exist. I could only guess
which object it is, as you didn't say which line it is. There are quite a
few object references in your code.

Chances are, the invalid cast exception is related to the null reference.
When you try to cast null as anything, it will throw that exception.


This is the lines that throws the NullReferenceException
int aktuelt_a_id = (int)(e.Item.DataItem as DataRowView).Row[11];
 
K

Kevin Spencer

Hi Martin,

That helps a lot! :)

Okay, there are a couple of problems with this line of code. A good rule of
thumb: Don't use the "as" operator to do your casting, unless you either
know for certain that the type is correct, or you account for the fact that
if the cast is incorrect, there will be no exception thrown, but the value
of the cast will be null.

In this case, that is exactly what happened, You tried to cast a DataItem as
a DataRowView. A DataItem can NOT be a DataRowView, so you have a null
reference to a non-existent DataRowView, which of course raises a
NullReferenceException when you attempt to identify a Row in it.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.

Martin Høst Normark said:
Kevin Spencer said:
Hi Martin,

Whichever line threw the exception is referencing an object that is null,
has not been instantiated, and/or does not yet exist. I could only guess
which object it is, as you didn't say which line it is. There are quite a
few object references in your code.

Chances are, the invalid cast exception is related to the null reference.
When you try to cast null as anything, it will throw that exception.


This is the lines that throws the NullReferenceException
int aktuelt_a_id = (int)(e.Item.DataItem as DataRowView).Row[11];
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top