Results 1 to 3 of 3

Thread: Object reference not set to an instance of an object

  1. #1
    KelvinN. is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    Nov 2015
    Location
    Nairobi
    Posts
    5
    Reputation
    0

    Object reference not set to an instance of an object

    Am trying to remove this error: Object reference not set to an instance of an object - what causes this error?

    Code:
        ''''' Remove words in Angle Brackets
    
            Private Function GetWordWithOutBracketedText(SelectedReplacement As String) As String
                ' check if the selected replacement contains bracket "("
                If Strings.InStr(SelectedReplacement, " (") = 0 Then
                    Return SelectedReplacement ' if not exists return the same string back
                End If
    
                ' if bracket is there in string split it with bracket
                Dim arr() As String = Strings.Split(SelectedReplacement, " (")
    
                ' send back the first element from splitted array as only first part is the replacement text
                Return arr(0)
    
            End Function
    When the contextMenuStrip appears, and I put this code on it here it never works:

    Code:
     Private Sub ContextMenuStrip1_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
    
    RichTextBox1.SelectedText = GetWordWithOutBracketedText(kamau)
    
    End Sub
    I get this error:

    Code:
    An unhandled exception of type 'System.NullReferenceException' occurred in System.Windows.Forms.dll
    
        Additional information: Object reference not set to an instance of an object.
    Last edited by KelvinN.; 03-16-2017 at 7:29 AM.

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,815
    Reputation
    1707
    When the exception is thrown, what line is it thrown on? What reference on that line is null (Nothing) at the time?

  3. #3
    KelvinN. is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    Nov 2015
    Location
    Nairobi
    Posts
    5
    Reputation
    0
    I solved this problem by putting it on a function that was not null.

Tags for this Thread

Bookmarks

Posting Permissions

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