locked
Very odd NullReferenceException RRS feed

  • Question

  • This is a very strange exception, indeed. When I run my form I get a NullReferenceException when calling DataGridView.CurrentRow.

     

    When the exception occurs, the program halts like normal and highlights the line that caused the error. This is what the line looks like:

    cmbConsumable.SelectedItem = dgvConsumables.CurrentRow.Cells(0).Value

    The exception helper tells me that CurrentRow is Nothing. It shouldn't be, so I mouse over dgvConsumables and expand the popup (the small window that shows dgvConsumables properties) and scroll down to CurrentRow. It indeed says the CurrentRow has an index of 0. Ok, whatever.

     

    I hit the Step Into button (F8) and, suddenly, the error no longer occurs and the code continues as expected. Essentially, CurrentRow was Nothing until I expanded the properties?

    What could be causing this to happen?

    Sunday, June 19, 2011 12:27 AM

All replies

  • Hi,

    If you are trying to select an item in a DataGridView row that is NOTHING or does not exist then later in the

    code the row gets added then it is assigned some data then you will continue to get this error I would imagine.

     

    I would try things in the following order:>>

     

    1. Try setting the DataGridView number of rows to the amount of data you wish to show.
    2. Insert the data you wish to show.
    3. Then use any SelectedItem  line of code or whatever. cmbConsumable.SelectedItem = dgvConsumables.CurrentRow.Cells(0).Value

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    XNA is coming to VB.Net




    Sunday, June 19, 2011 3:20 PM
  • cmbConsumable.SelectedItem = dgvConsumables.CurrentRow.Cells(0).Value

     

    dgvConsumables.CurrentRow.Cells(0).Value

    Try

    dgvConsumables.CurrentRow.Cells(0).Value.ToString

    You Could also just try using "On Error Resume Next", maybe that will "fix" your problem...

    Does This Help Any?

     


    If you want something you've never had, you need to do something you've never done.
    Tuesday, June 21, 2011 7:55 PM
    Moderator
  • Yea there is something wrong in your code, but as long as you are not showing that code we never will really know what it was (even not if you tell that it does not appear anymore without showing the wrong code).

     


    Success
    Cor
    Wednesday, June 22, 2011 5:40 AM