Results 1 to 15 of 15

Thread: [RESOLVED] DataGridView - Header Sort - Object not set to Instance (null)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    178

    Resolved [RESOLVED] DataGridView - Header Sort - Object not set to Instance (null)

    I have data that I am loading from an SQLite DB.

    If I click on the header to sort, I get an Object not set to instance error.

    I used the below code to try and catch the null error, but it still keeps generating the error.

    What am I doing wrong??
    I also would like to sort the datagrid too after clicking the column heading.

    Code:
    Try
        Dim row As Integer = dgrid1.CurrentRow.Index
    Catch
        Exit Sub
    Finally
        'continue on
    End Try
    Thanks in advance
    Last edited by pixelink; Sep 16th, 2020 at 12:04 PM.
    Can't Type - Forgetful - Had Stroke = Forgive this old man!

    Pixelink Media
    VSCOMM 2019 LAZARUS 2.08 Win10 16G RAM Nivida GForce RTX 2060

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,483

    Re: DataGridView - Header Sort - Object note set to Instance (null)

    I'm not sure how you think that that code could do anything useful, given that there's no sorting going on in the Try block. If the exception is thrown when you click the column header then either it's being thrown in system code or else you have provided code to sort programmatically and it's being thrown in that code. Have you provided such code? If so, show us that. Regardless of anything else, look at the stack trace for the exception, which the Exception Assistant window will provide if this is occurring in the debugger. If it's not in the debugger, you should be handling the UnhandledException event of the application in order to shut down cleanly on an unexpected exception anyway and you can get the stack trace in that event handler.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    178

    Re: DataGridView - Header Sort - Object note set to Instance (null)

    Quote Originally Posted by jmcilhinney View Post
    I'm not sure how you think that that code could do anything useful, given that there's no sorting going on in the Try block. If the exception is thrown when you click the column header then either it's being thrown in system code or else you have provided code to sort programmatically and it's being thrown in that code. Have you provided such code? If so, show us that. Regardless of anything else, look at the stack trace for the exception, which the Exception Assistant window will provide if this is occurring in the debugger. If it's not in the debugger, you should be handling the UnhandledException event of the application in order to shut down cleanly on an unexpected exception anyway and you can get the stack trace in that event handler.
    The data is already sorted and showing when I fetch it from the DB.

    But, when I click the header I want to resort it in the DGV view too

    This exception is only happening when I click a column header.

    I believe that the row is null... but I will check ot your suggestions to see exactly what is happening
    Can't Type - Forgetful - Had Stroke = Forgive this old man!

    Pixelink Media
    VSCOMM 2019 LAZARUS 2.08 Win10 16G RAM Nivida GForce RTX 2060

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    178

    Re: DataGridView - Header Sort - Object note set to Instance (null)

    What I don't understand is that I have a completely different app that doesn't throw this error.... and I copy/pasted the DGV control and code over to this project.
    I don't understand why that DGV works fine and this one doesn't

    I am still double-checking the difference between the apps to see if I forgot something
    Last edited by pixelink; Sep 16th, 2020 at 04:13 AM.
    Can't Type - Forgetful - Had Stroke = Forgive this old man!

    Pixelink Media
    VSCOMM 2019 LAZARUS 2.08 Win10 16G RAM Nivida GForce RTX 2060

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    178

    Re: DataGridView - Header Sort - Object note set to Instance (null)

    Update code... same error

    Code:
    Private Sub dgrid1_SelectionChanged(sender As Object, e As EventArgs) Handles dgrid1.SelectionChanged
    
            Try
                Dim row As Integer = dgrid1.CurrentRow.Index  '<<=== ERROR EXCEPTION
            Catch ex As Exception
                Exit Sub
            Finally
                'continue on
            End Try
    End Sub
    Can't Type - Forgetful - Had Stroke = Forgive this old man!

    Pixelink Media
    VSCOMM 2019 LAZARUS 2.08 Win10 16G RAM Nivida GForce RTX 2060

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,483

    Re: DataGridView - Header Sort - Object note set to Instance (null)

    Quote Originally Posted by pixelink View Post
    The data is already sorted and showing when I fetch it from the DB.

    But, when I click the header I want to resort it in the DGV view too

    This exception is only happening when I click a column header.

    I believe that the row is null... but I will check ot your suggestions to see exactly what is happening
    That doesn't address anything that I posted. Let's start with the obvious question. Is this exception being thrown in the debugger or not? If it is then the debugger is giving you all the information about that exception so look at that information and, in particular, the stack trace in order to determine where the exception is actually being thrown. Isn't where the exception is thrown an important thing to know if you're trying to diagnose it? Whether or not it's being thrown in the debugger, you should be handling the UnhandledException event of the application. Are you doing that? If not, do it. If the exception is not being thrown in the debugger then you can get the same information in that event handlert.

    In summary, get the stack trace for the exception and find out where the exception is being thrown. Trying to diagnose an exception without knowing where it's being thrown is a problem, don't you think?

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    178

    Re: DataGridView - Header Sort - Object note set to Instance (null)

    FYI... my SQL code that fetches and sorts my data before I click the column header (then get error)
    Getting and sorting the data in code works perfectly fine... it display in my DGV perfectly

    Code:
    Sub showData()
            Dim filePath = lbStatus.Text  'path to *.db3
            Using connection As New SQLite.SQLiteConnection("Data Source=" & filePath & "")
    
                Dim da As New SQLiteDataAdapter("select * from mainglist ORDER BY CAST(Category As String)", connection)
                Dim dt As New DataTable
                Dim ds As New DataSet
                da.Fill(ds, "mainglist")
    
                dgrid1.DataSource = ds
                dgrid1.DataMember = "mainglist"
    
                dt = ds.Tables(0)
                Dim myDataView As DataView = dt.DefaultView
                myDataView.Sort = "Category"
                dgrid1.DataSource = myDataView
    
                connection.Close()
                da.Dispose()
            End Using
    
        End Sub
    Can't Type - Forgetful - Had Stroke = Forgive this old man!

    Pixelink Media
    VSCOMM 2019 LAZARUS 2.08 Win10 16G RAM Nivida GForce RTX 2060

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    178

    Re: DataGridView - Header Sort - Object note set to Instance (null)

    Quote Originally Posted by jmcilhinney View Post
    That doesn't address anything that I posted. Let's start with the obvious question. Is this exception being thrown in the debugger or not? If it is then the debugger is giving you all the information about that exception so look at that information and, in particular, the stack trace in order to determine where the exception is actually being thrown. Isn't where the exception is thrown an important thing to know if you're trying to diagnose it? Whether or not it's being thrown in the debugger, you should be handling the UnhandledException event of the application. Are you doing that? If not, do it. If the exception is not being thrown in the debugger then you can get the same information in that event handlert.

    In summary, get the stack trace for the exception and find out where the exception is being thrown. Trying to diagnose an exception without knowing where it's being thrown is a problem, don't you think?
    Yes, in the debugger.

    As I already said... It is looking for a row index, but there is none.

    From Debugger

    System.NullReferenceException: 'Object reference not set to an instance of an object.'

    System.Windows.Forms.DataGridView.CurrentRow.get returned Nothing.
    I thought I am trying to handle the exception with the Try/Catch code... is that wrong??

    The exception is being thrown on the line in code in Post #5
    Dim row As Integer = dgrid1.CurrentRow.Index <<== ERROR EXCEPTION IS THROWN HERE
    Last edited by pixelink; Sep 16th, 2020 at 04:26 AM.
    Can't Type - Forgetful - Had Stroke = Forgive this old man!

    Pixelink Media
    VSCOMM 2019 LAZARUS 2.08 Win10 16G RAM Nivida GForce RTX 2060

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,483

    Re: DataGridView - Header Sort - Object note set to Instance (null)

    OK, I didn't see post #5 when I posted earlier. You were typing that while I was typing post #6. It would have helped if you had provide a full and clear explanation of the problem in the first place. The issue has nothing specific to do with sorting. It's thrown in the SelectionChanged event handler and that's what you should have told us in the first place. If the exception is thrown on that line then, as is always the case with a NulReferenceException, it means that something before a dot is Nothing. That something is almost certain dgrid1.CurrentRow. When you sort the selection changes and there is presumably no current row when that happens, probably because all rows are removed before being re-added in the appropriate order. The obvious solution is to not just assume that there's a current row but to actually check whether there is one:
    vb.net Code:
    1. Private Sub dgrid1_SelectionChanged(sender As Object, e As EventArgs) Handles dgrid1.SelectionChanged
    2.     Dim row = dgrid1.CurrentRow
    3.  
    4.     If row IsNot Nothing Then
    5.         'There is a current row so it is safe to use it.
    6.         Dim rowIndex = row.Index
    7.  
    8.         'Use rowIndex here.
    9.     End If
    10. End Sub
    Prevention is better than cure, i.e. you should almost always validate to prevent an exception being thrown rather than clean up afterwards.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    178

    Re: DataGridView - Header Sort - Object note set to Instance (null)

    Quote Originally Posted by jmcilhinney View Post
    OK, I didn't see post #5 when I posted earlier. You were typing that while I was typing post #6. It would have helped if you had provide a full and clear explanation of the problem in the first place. The issue has nothing specific to do with sorting. It's thrown in the SelectionChanged event handler and that's what you should have told us in the first place. If the exception is thrown on that line then, as is always the case with a NulReferenceException, it means that something before a dot is Nothing. That something is almost certain dgrid1.CurrentRow. When you sort the selection changes and there is presumably no current row when that happens, probably because all rows are removed before being re-added in the appropriate order. The obvious solution is to not just assume that there's a current row but to actually check whether there is one:
    vb.net Code:
    1. Private Sub dgrid1_SelectionChanged(sender As Object, e As EventArgs) Handles dgrid1.SelectionChanged
    2.     Dim row = dgrid1.CurrentRow
    3.  
    4.     If row IsNot Nothing Then
    5.         'There is a current row so it is safe to use it.
    6.         Dim rowIndex = row.Index
    7.  
    8.         'Use rowIndex here.
    9.     End If
    10. End Sub
    Prevention is better than cure, i.e. you should almost always validate to prevent an exception being thrown rather than clean up afterwards.
    That code you provided doesn't work.

    It is passing it altogether and the exception happens on this line

    Code:
    'this is where I add data to text controls for viewing and editing
    cbCategory.Text = CStr(dgrid1.CurrentRow.Cells(1).Value)
    BUT... I JUST DISCOVERED SOMETHING....


    1) The exception is only happening in Debug mode... not Release mode.

    2) When I compile in Release mode the Clicking on DGV works at runtime

    Not sure why the different states are effecting exceptions.
    Can't Type - Forgetful - Had Stroke = Forgive this old man!

    Pixelink Media
    VSCOMM 2019 LAZARUS 2.08 Win10 16G RAM Nivida GForce RTX 2060

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    178

    Re: DataGridView - Header Sort - Object note set to Instance (null)

    So, I went to check my other app that uses the same DGV and sqlite... same thing.... Exception only happens in Debug mode.


    This is new to me, so how to I properly handle this.

    Even though it works fine in Release mode, should I be concerned when in Debug mode.
    I kind of don't want to have that be an issue though
    Can't Type - Forgetful - Had Stroke = Forgive this old man!

    Pixelink Media
    VSCOMM 2019 LAZARUS 2.08 Win10 16G RAM Nivida GForce RTX 2060

  12. #12
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,483

    Re: DataGridView - Header Sort - Object note set to Instance (null)

    Quote Originally Posted by pixelink View Post
    That code you provided doesn't work.

    It is passing it altogether and the exception happens on this line

    Code:
    'this is where I add data to text controls for viewing and editing
    cbCategory.Text = CStr(dgrid1.CurrentRow.Cells(1).Value)
    BUT... I JUST DISCOVERED SOMETHING....


    1) The exception is only happening in Debug mode... not Release mode.

    2) When I compile in Release mode the Clicking on DGV works at runtime

    Not sure why the different states are effecting exceptions.
    Argh! That means that the code I provided IS working. It "is passing it altogether" specifically because there is no current row and yet you now have even more code that assumes that there is. I specifically demonstrated that you need to test whether there is a current row and NOT try to use a current row when there isn't and then you go and try to use the current row:
    Code:
    cbCategory.Text = CStr(dgrid1.CurrentRow.Cells(1).Value)
    We have already established that that is Nothing and that you should NOT use it in that case and you've just ignored that. The idea is that you use row inside the If block and ONLY inside the If block.

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    178

    Re: DataGridView - Header Sort - Object note set to Instance (null)

    Fixed...

    I tweaked your code to...

    Code:
    Dim row = dgrid1.CurrentRow
    If row Is Nothing Then
       Exit Sub
    End If
    THANKS JMC.... once again you have been a great help!!!
    Can't Type - Forgetful - Had Stroke = Forgive this old man!

    Pixelink Media
    VSCOMM 2019 LAZARUS 2.08 Win10 16G RAM Nivida GForce RTX 2060

  14. #14
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,483

    Re: [RESOLVED] DataGridView - Header Sort - Object note set to Instance (null)

    Let's try it in a slightly different way that you can't muck up even if you don't understand it:
    vb.net Code:
    1. Private Sub dgrid1_SelectionChanged(sender As Object, e As EventArgs) Handles dgrid1.SelectionChanged
    2.     Dim row = dgrid1.CurrentRow
    3.  
    4.     If row Is Nothing Then
    5.         'There is no current row so exit
    6.         Return
    7.     End If
    8.  
    9.     'If you get to here then there is a current row and the 'row' variable refers to it so use 'row' here.
    10.     '...
    11. End Sub
    Any code that needs to use the current row replaces that last ellipsis and it uses the row variable to refer to it.

    EDIT: made a small but critical correction to the code.
    Last edited by jmcilhinney; Sep 16th, 2020 at 05:05 AM. Reason: Changed IsNot operator top Is.

  15. #15
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,483

    Re: DataGridView - Header Sort - Object note set to Instance (null)

    Quote Originally Posted by pixelink View Post
    Fixed...

    I tweaked your code to...

    Code:
    Dim row = dgrid1.CurrentRow
    If row Is Nothing Then
       Exit Sub
    End If
    THANKS JMC.... once again you have been a great help!!!
    Seems you beat me to it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width