DatagridView NullReferenceExeption

Huascar82

New member
Joined
Oct 3, 2007
Messages
3
Programming Experience
Beginner
Hey All,

I've got a datagridview populated with data from a database. I'm trying to run this piece of code to get data from one of the cell. It works but when I reorder the grid by clicking on the column header I get a

"A first chance exception of type 'System.NullReferenceException' occurred"
and it points to the only line of code in the sub
VB.NET:
Private Sub dgvMasterList_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgvMasterList.SelectionChanged
        txtOldAssetTag.Text = dgvMasterList.Rows(dgvMasterList.CurrentRow.Index).Cells(7).Value()
    End Sub

no idea what this could be.

Please help.
Thanks


*******EDIT******
Don't know who did it, but I appreciate the move. Sorry for posting to the wrong section.
 
Last edited:
Click Debug menu
CLick Exceptions
Tick the THROWN column of CLR exceptions
Run your code
Reproduce the crash
Code halts at the point of error

Now examine everything on that line before a period (full stop, dot, . )
One of those things is null.

i.e. literally point your mouse at the word txtOldAssetTag
Does it say "NOTHING" in the tooltip, or does it say "Windows.Forms.TextBox" ?
If it says NOTHING, then the variable is nothing, null, etc so you cant call a method or property like .Text on it

Point to everything else! You might have to highlight some things, like highlight dgvMasterList.CurrentRow so it goes blue, then point to the selection? Is that Nothing?


OK, so find your element that is set to null, and either put a check in so the code doesnt run, or find out why its Null/Nothing and sort that out

(How to use the debugger 101)
 
here's the full details of the error message. Maybe this will help someone help me. :)

I understood the basic meaning of the error. something somewhere was returning a null value and that was kicking the error. I thought maybe someone may have seen this before since I assumed it was the logical way of getting data from a row populated to text fields. this is pretty much my first app in VB.net of this complexity with data work. If I knew how to write the code to handle the error better I would do that so maybe someone can show me how it should be done.
I think the exception is happening when I try to to get the value of dgvMasterList.CurrentRow.Index
I would assume that since the data is reorganizing itself once I click on the column heading it looses its point in the grid and therefore there would be no index value.
Right now I pretty much have it with an "On error resume next". I know this is dirty and inefficient code but I just don't know how to handle it. If someone could help that would be appreciated.

System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."
Source="PC Replacement FrontEnd"
StackTrace:
at PC_Replacement_FrontEnd.frmEntireList.dgvMasterList_SelectionChanged(Object sender, EventArgs e) in G:\Visual Studio 2005\Projects\PC Replacement FrontEnd ACCESS DB\PC Replacement FrontEnd\frmEntireList.vb:line 94
at System.Windows.Forms.DataGridView.OnSelectionChanged(EventArgs e)
at System.Windows.Forms.DataGridView.OnRowCollectionChanged_PostNotification(Boolean recreateNewRow, Boolean allowSettingCurrentCell, CollectionChangeAction cca, DataGridViewRow dataGridViewRow, Int32 rowIndex)
at System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged_PostNotification(CollectionChangeAction cca, Int32 rowIndex, Int32 rowCount, DataGridViewRow dataGridViewRow, Boolean changeIsDeletion, Boolean changeIsInsertion, Boolean recreateNewRow, Point newCurrentCell)
at System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged(CollectionChangeEventArgs e, Int32 rowIndex, Int32 rowCount, Boolean changeIsDeletion, Boolean changeIsInsertion, Boolean recreateNewRow, Point newCurrentCell)
at System.Windows.Forms.DataGridViewRowCollection.ClearInternal(Boolean recreateNewRow)
at System.Windows.Forms.DataGridView.RefreshRows(Boolean scrollIntoView)
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.ProcessListChanged(ListChangedEventArgs e)
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.currencyManager_ListChanged(Object sender, ListChangedEventArgs e)
at System.Windows.Forms.CurrencyManager.OnListChanged(ListChangedEventArgs e)
at System.Windows.Forms.CurrencyManager.List_ListChanged(Object sender, ListChangedEventArgs e)
at System.Data.DataView.OnListChanged(ListChangedEventArgs e)
 
Last edited by a moderator:
CurrentRow can be Nothing. (If dgv.CurrentRow IsNot Nothing Then... it's ok to access CurrentRow object).
 
Back
Top