locked
Null Reference Exception on an Invoke RRS feed

  • Question

  • This one has me stumped (not that I pretend to understand Delegates ).   I have a process that is on a seperate thread in a class module, that class module raises an event, which the main form repsonds to and then updates a listview based on the event.  So far, so good.

    The problem arises in my routine in the main form to update the listview, which as far as I can tell I have structured correctly.  The .Invoke line (bolded below) causes a System.NullReferenceException ("Object is not set to an instance of an object.").  I'm at a complete loss to understand how that particular error can be generated by an Invoke statement:

    Private Delegate Sub DEL_UpdateListItem(ByVal RowIndex As Integer, ByVal ColumnKey As String, ByVal TextValue As String)
    Private Sub UpdateListItem(ByVal RowIndex As Integer, ByVal ColumnKey As String, ByVal TextValue As String)
         If lvwConns.InvokeRequired Then
              Dim poCB As New DEL_UpdateListItem(AddressOf UpdateListItem)
              Dim poParm(2) As Object
              poParm(0) = RowIndex
              poParm(1) = ColumnKey
              poParm(2) = TextValue
              lvwConns.Invoke(poCB, poParm)
         Else
              If ColumnKey.ToUpper = "STATE" Then
                   lvwConns.Items(RowIndex).Text = TextValue
              Else
                   lvwConns.Items(RowIndex).SubItems(ColumnKey).Text = TextValue
              End If
         End If
    End Sub

    I have a similar routine that changes the associated Icon for the row, and it seems to work fine.  Any and all suggestions as to how to correct this are welcome

    - Richard

    Wednesday, May 10, 2006 11:37 PM

Answers

  • Tim, thanks for the response.   The call stack tracked back to the raiseevent that triggered the routine that called the block of code I posted.  The null reference error appears to be spurious. 

    Everything in that if-block had a valid object reference, hence my confusion.  After commenting out different lines to see what was causing it, I figured out the error was actually occurring in the block below the invoke.  I had tried to address the listview sub-items as you would a datagridview cell which produced an error but it was being reported by the invoke line.... /shrug

    Once I changed how I was setting the subitem text values, it worked fine.  Thanks for the suggestion though!

     

    Richard

    Thursday, May 11, 2006 8:03 PM

All replies

  • Hi Richard,

    If you use F5 to debug and launch the application, and then get to the point where the exception shows up (the exception assistant should say "NullReferenceException"), can you get the call stack information? The exception assistant should also give you a hint as to which object was expected to be non-Null.

    With that information, we could try to see what is causing the NullReferenceException to be thrown.

    Tim
    Thursday, May 11, 2006 6:54 PM
    Moderator
  • Tim, thanks for the response.   The call stack tracked back to the raiseevent that triggered the routine that called the block of code I posted.  The null reference error appears to be spurious. 

    Everything in that if-block had a valid object reference, hence my confusion.  After commenting out different lines to see what was causing it, I figured out the error was actually occurring in the block below the invoke.  I had tried to address the listview sub-items as you would a datagridview cell which produced an error but it was being reported by the invoke line.... /shrug

    Once I changed how I was setting the subitem text values, it worked fine.  Thanks for the suggestion though!

     

    Richard

    Thursday, May 11, 2006 8:03 PM
  • Hi,

     

    what exactly is the reason that sometimes the .invoke call is producing such errors? I have observed exactly the same problem (the error occurring in the block below the invoke) and I am not able to solve the problem because I need more complex interaction from the main UI thread before my invoke calling thread is allowed to go on with its work. Why is a "simple" call through invoke possible but if extending the lines below the invoke not? I am lost and need some basic understanding.

     

    More specifically I have posted a question concerning this issue at http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2370684&SiteID=1&mode=1

     

    Best regards

    Alex

     

    Tuesday, November 6, 2007 11:47 AM