Results 1 to 11 of 11

Thread: [RESOLVED] Object reference not set to an instance of an object.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2016
    Posts
    279

    Resolved [RESOLVED] Object reference not set to an instance of an object.

    I have 2 functions where in the first function will bind database to datagridview using .datasource and datatable. The second function will output the
    last cell value of the data grid view. But when I tried to run it, I received an error "Object reference not set to an instance of an object.". I wonder why am I receiving that error because I already bind the data to dgv so I am expecting there is a data inside of dgv.

    Here is the 2 functions that I used:

    Code:
     Private Sub checkSampling()
            Dim x As Integer = DataGridView1.Rows.Count - 1
            Dim str As String = DataGridView1.Rows(x).Cells(0).Value.ToString
            sampling = str
            MsgBox(sampling)
        End Sub

    Code:
    Private Sub displayGrid()
            Using connection As New MySqlConnection(connectionstring)
                SQL = "Select full_model,parameter,created_on,reading, mrdata,samplingtime from IPQC_'" & month.Text & "' where full_model='" & lblModel.Text & "' and parameter='" & tempparam & "' "
                Using da As New MySqlDataAdapter(SQL, connection)
                    Dim dt As New DataTable
                    da.Fill(dt)
                    DataGridView1.DataSource = dt
                   
                End Using
            End Using
        End Sub
    The error is this line:
    Code:
     Dim str As String = DataGridView1.Rows(x).Cells(0).Value.ToString

    HAPPY NEW YEAR VBFORUMS
    Last edited by earvinnill; Jan 3rd, 2017 at 09:57 PM.

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

    Re: Object reference not set to an instance of an object.

    When the exception is thrown, the IDE will tell you exactly which line it was thrown on. You're provided with that information to help you diagnose the issue. If you want us to help you do that then why are you keeping that information secret?

    Before posting here at all though, you should first be determining what reference on that line is null. You have the code running and the exception just thrown right there in the debugger for you so don't ignore all the information it's providing you. Did you mouse over each reference to see what their values are and which is Nothing? If so, which one was the issue? Of not, why not? Do all you can for yourself first and then, if you still can't work it out, provide us with ALL the relevant information when you post here.

    I would guess that the cell you're referring to contains no data thus its Value property is Nothing, thus calling ToString throws the exception. It will take you seconds to confirm whether that's the case or not if you use the debugger.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2016
    Posts
    279

    Re: Object reference not set to an instance of an object.

    Quote Originally Posted by jmcilhinney View Post
    .

    I forgot to post the line that has an error. " Dim str As String = DataGridView1.Rows(x).Cells(0).Value.ToString" and yes your guess is right that the cell contains no data. But is that happening?

  4. #4
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    285

    Re: Object reference not set to an instance of an object.

    Is that cell supposed to have an empty state?

    If so, I believe you can fix your problems with this line of code:

    Code:
    If DataGridView1.Rows(x).Cells(0).Value IsNot Nothing Then
        Dim str As String = DataGridView1.Rows(x).Cells(0).Value.ToString
    End If
    If the fact that the cell IS empty is not expected, then your problem lies elsewhere.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,428

    Re: Object reference not set to an instance of an object.

    it's late, and I'm tired so I'm not going to give it too much thought... but ... I suspect that the Rows.Count also includes the "New Row" that's at the bottom of the grid ... so what the OP is getting is the cell that's in this empty row, all of the cells of which would be Nothing.
    Rather than getting the data from the grid, try getting it from the datatable directly instead.
    Just a thought.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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

    Re: Object reference not set to an instance of an object.

    Quote Originally Posted by Nirwanda View Post
    Is that cell supposed to have an empty state?

    If so, I believe you can fix your problems with this line of code:

    Code:
    If DataGridView1.Rows(x).Cells(0).Value IsNot Nothing Then
        Dim str As String = DataGridView1.Rows(x).Cells(0).Value.ToString
    End If
    If the fact that the cell IS empty is not expected, then your problem lies elsewhere.
    I would suggest that, if the cell being empty is a valid state, a possibly better option is to use CStr, which doesn't choke on Nothing. If you pass Nothing in the it returns Nothing.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,130

    Re: Object reference not set to an instance of an object.

    Quote Originally Posted by techgnome View Post
    I suspect that the Rows.Count also includes the "New Row" that's at the bottom of the grid ... so what the OP is getting is the cell that's in this empty row, all of the cells of which would be Nothing.
    I suspect so too but, if that is the case, it should be obvious simply by looking at the grid itself. If you're posting a question here without actually looking at the control that is causing the issue then you probably ought to be spending a bit more time trying to solve the issue yourself first.
    Quote Originally Posted by techgnome View Post
    Rather than getting the data from the grid, try getting it from the datatable directly instead.
    I would actually suggest binding to a BindingSource first, binding that to the grid and then using it for what it's intended, i.e. the one place that you access the bound data. Calling Last or LastOrDefault on the BindingSource will give you the last record.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2016
    Posts
    279

    Re: Object reference not set to an instance of an object.

    Quote Originally Posted by jmcilhinney View Post
    I would actually suggest binding to a BindingSource first, binding that to the grid and then using it for what it's intended, i.e. the one place that you access the bound data. Calling Last or LastOrDefault on the BindingSource will give you the last record.
    Hi, I've saw what is the cause of the error and it is the rows.count, i tried getting other cell value and it is working. the problem is I wanted to retrieve the data from the last row but as we know that I am having an error when using the row.count-1 in dgv.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2016
    Posts
    279

    Re: Object reference not set to an instance of an object.

    Quote Originally Posted by techgnome View Post
    it's late, and I'm tired so I'm not going to give it too much thought... but ... I suspect that the Rows.Count also includes the "New Row" that's at the bottom of the grid ... so what the OP is getting is the cell that's in this empty row, all of the cells of which would be Nothing.
    Rather than getting the data from the grid, try getting it from the datatable directly instead.
    Just a thought.

    -tg

    yes the rows.count is the suspect of the error, i tried retrieving other cell values and it is working. so the new row is with the rows.count.

  10. #10
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    477

    Re: Object reference not set to an instance of an object.

    The error is actually caused by this line:

    Code:
            Dim x As Integer = DataGridView1.Rows.Count - 1
    Change the -1 to -2. This is because by default the last row is always empty. With -1 you will always be reading the last row which will always be empty. The alternative is to turn off the entry row option (I forget what it is called).

    I should also point out that the way your code works, no matter how many times you call that sub, you will always be reading the first cell of the last line (second to last line with the change I suggested). If you ever want to read a different line you will need to change your code.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2016
    Posts
    279

    Re: Object reference not set to an instance of an object.

    Quote Originally Posted by Maverickz View Post
    The error is actually caused by this line:

    Code:
            Dim x As Integer = DataGridView1.Rows.Count - 1
    Change the -1 to -2. This is because by default the last row is always empty. With -1 you will always be reading the last row which will always be empty. The alternative is to turn off the entry row option (I forget what it is called).

    I should also point out that the way your code works, no matter how many times you call that sub, you will always be reading the first cell of the last line (second to last line with the change I suggested). If you ever want to read a different line you will need to change your code.

    Thank you for your advice

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