2

I would like to ask how I can contain the error I am getting "Object reference not set to an instance of an object.",nullreferenceException .I am trying to assign the value I am getting from the gridview to a variable. I get the error on the first line. how can I handle a situation when the value is null.I've tried using isnot Nothing but its still giving me the error and isdbnull wont work because I am not dealing with a datatable .

I only get this error when the gridview is still empty how can I handle this.

If Not IsNothing(ProductsRawMaterialGrid.GridViewElement.CurrentRow.Cells("PercentageInMix").Value) Then
    PIM = ProductsRawMaterialGrid.GridViewElement.CurrentRow.Cells("PercentageInMix").Value
Else
    PIM = FormatNumber("0.00", 2)
End If
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
Tendai Mare
  • 31
  • 1
  • 2
  • 14
  • possible duplicate of [Using VB.NET IIF I get NullReferenceException](http://stackoverflow.com/questions/428959/using-vb-net-iif-i-get-nullreferenceexception) – Stefan Aug 01 '12 at 09:01
  • Not a exact duplicate, but the accepted answer on the other question will work perfectly for this question. – Stefan Aug 01 '12 at 09:02
  • Perhaps your problem start with CurrentRow when the grid is empty. That's the object reference that fails. – Steve Aug 01 '12 at 09:05
  • @Stefan: No, it won't. The other question is about an operator not short-circuiting; the problem in this question is that there's not enough null checking. – Dan Puzey Aug 01 '12 at 09:06
  • @DanPuzey, true, the 'question' itself is not a duplicate, but the answer on that question would solve this questions problem. – Stefan Aug 01 '12 at 11:59
  • How does that answer help? You'd need 5 `If` statements to protect the full expression ProductsRawMaterialGrid.GridViewElement.CurrentRow.Cells("PercentageInMix").Value` from null refs. The answer you linked explains how to use `If` as a short-circuited version of `Iif`, which is a different problem - there's no issue with (lack of) short-circuiting here. – Dan Puzey Aug 01 '12 at 12:03
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders May 15 '14 at 03:07

1 Answers1

1

The problem is that you don't know what is null in your line of code. Considering the problem line, you have this expression:

ProductsRawMaterialGrid.GridViewElement.CurrentRow.Cells("PercentageInMix").Value

There are six things that could evaluate null in that expression:

  • ProductsRawMaterialGrid could be null
  • ProductsRawMaterialGrid.GridViewElement could be null
  • ProductsRawMaterialGrid.GridViewElement.CurrentRow could be null
  • ProductsRawMaterialGrid.GridViewElement.CurrentRow.Cells could be null
  • ProductsRawMaterialGrid.GridViewElement.CurrentRow.Cells("PercentageInMix") could be null
  • ProductsRawMaterialGrid.GridViewElement.CurrentRow.Cells("PercentageInMix").Value could be null

You are only checking one of them (the last one). Unfortunately if any of the others are null, you will see a NullReferenceException.

An educated guess would be that either CurrentRow is null when you call the code, or that your cell name is wrong and .Cells("PercentageInMix") is null. Splitting this code out or examining it in the debugger should help you resolve the issue.

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
  • Indeed, but the OP used `null` so I'm assuming they worked that out :-) Always thought it was silly using `Nothing` in VB because it makes `NullReferenceException` a little meaningless! – Dan Puzey Aug 01 '12 at 10:10