Unhandled exception - "Object reference not set to an instance of an object"

Jan 25, 2010 at 9:59 AM

Hello all.

I'd really appreciate some help here as I am stuck.  I am a nooob developer, so please forgive any ignorance.

I have a SourceGrid which holds some data.  I have no problem setting up the grid and populating the grid with data.  (This grid really works very well).

However I have one irritating problem.  If the user has another object selected (say a textbox on the form), and then selects a cell in the SourceGrid, they get the error "Object reference not set to an instance of an object".  If they continue past the error, then it works fine until they again select another object on the form and come back to the SourceGrid.

Here is the code used to Setup the Grid :

Private Sub SetupHistoryGrid()
        With GridCompany_Activities
            .Redim(1, 13)
            .BorderStyle = BorderStyle.Fixed3D
            .BackColor = Color.White
            .VScrollBar.Enabled = True
            .FixedRows = 1

            .SelectionMode = SourceGrid.GridSelectionMode.Row
            .Columns(0).Width = 35
            .Columns(1).Width = 70
            .Columns(2).Width = 70
            .Columns(3).Width = 120
            .Columns(4).Width = 300
            .Columns(5).Width = 25
            .Columns(6).Width = 20
            .Columns(7).Width = 60
            .Columns(8).Width = 120
            .Columns(9).Width = 50
            .Columns(10).Width = 100
            .Columns(11).Width = 80
            .Columns(12).Width = 80

            Dim Sorter1 As New SourceGrid.Cells.ColumnHeader("Date")
            Sorter1.SortComparer = New SourceGrid.MultiColumnsComparer(1, 2)

            .Item(0, 0) = New SourceGrid.Cells.ColumnHeader("Type")
            .Item(0, 1) = Sorter1
            .Item(0, 2) = New SourceGrid.Cells.ColumnHeader("Time")
            .Item(0, 3) = New SourceGrid.Cells.ColumnHeader("Candidate")
            .Item(0, 4) = New SourceGrid.Cells.ColumnHeader("Details")
            .Item(0, 5) = New SourceGrid.Cells.ColumnHeader("Usr")
            .Item(0, 6) = New SourceGrid.Cells.ColumnHeader("Follow Up")
            .Item(0, 7) = New SourceGrid.Cells.ColumnHeader("Next Date")
            .Item(0, 8) = New SourceGrid.Cells.ColumnHeader("Next Details")
            .Item(0, 9) = New SourceGrid.Cells.ColumnHeader("Next Priority")
            .Item(0, 10) = New SourceGrid.Cells.ColumnHeader("Result")
            .Item(0, 11) = New SourceGrid.Cells.ColumnHeader("Associated")
            .Item(0, 12) = New SourceGrid.Cells.ColumnHeader("ContactID")
            .AutoScroll = False
        End With

    End Sub
To try to trouble shoot this problem, I have removed all other code associated with this SourceGrid 
(ie there is no click, double click, got focus, enter etc event) other than setting up the grid and populating it with data, and yet it still happens.
Any advice will be appreciated. 
Thanks
Lloyd
Jan 25, 2010 at 10:10 AM

Isn't it weird how when you take the trouble to ask the question, you seem to find the answer yourself.

The problem was the <b>.SelectionMode = SourceGrid.GridSelectionMode.Row </b> line.

Removed that and it seems to be working perfectly now.

Thanks for your help anyway.