none
Null reference exception was unhandeled RRS feed

  • Question

  • Please Help!

    I'm fairly new to VB.NET

    When checking the datagridview cell content,

    The following code brings this 'Null Reference Exception Was Unhandled' error at the end of the loop count. It works for first 52 loops but at the second last loop it throws the error mentioned.

    For N2 = 0 To dgEFdataGridView.Rows.Count - 1
               ' exit if the cell is empty
               If dgEFdataGridView.Rows(N2).Cells("Watts").Value.ToString Is Nothing Then
                    Exit Sub
                'End If
     Next

    Any help would be appreciated.

    Thanks

    Sashi

    Friday, June 1, 2012 6:04 PM

Answers

  • Did you misspell "Watts" and that is returning null?

    Before that if do a check for null on dgEFdataGridView.Rows(N2).Cells("Watts") before attempting to access the Value property. It is standard to check for nulls at each step of the way.


    William Wegerson (www.OmegaCoder.Com)


    • Edited by OmegaManModerator Friday, June 1, 2012 6:08 PM
    • Proposed as answer by MasaSam Friday, June 1, 2012 6:30 PM
    • Marked as answer by newLerner Friday, June 1, 2012 7:23 PM
    Friday, June 1, 2012 6:08 PM
    Moderator

All replies

  • Did you misspell "Watts" and that is returning null?

    Before that if do a check for null on dgEFdataGridView.Rows(N2).Cells("Watts") before attempting to access the Value property. It is standard to check for nulls at each step of the way.


    William Wegerson (www.OmegaCoder.Com)


    • Edited by OmegaManModerator Friday, June 1, 2012 6:08 PM
    • Proposed as answer by MasaSam Friday, June 1, 2012 6:30 PM
    • Marked as answer by newLerner Friday, June 1, 2012 7:23 PM
    Friday, June 1, 2012 6:08 PM
    Moderator
  • Thanks OmegaMan now it works.


    sashi

    Friday, June 1, 2012 6:26 PM
  • Hi Sashi,

    In the MSDN forums when an answer is found, you the OP should mark the post which helped as "answered" so if others search for the issue they will see it is resolved.


    William Wegerson (www.OmegaCoder.Com)

    Friday, June 1, 2012 7:13 PM
    Moderator