Results 1 to 5 of 5

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

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    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 nqioweryuadfge; Mar 16th, 2017 at 06:38 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

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

    What line was the exception thrown on and what reference on the line was null (Nothing) when it was thrown?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

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

    I solved the problem by researching online what causes these errors. Fact is! You have to reference an object like a
    Code:
    RichTextBox.SelectedText = GetSomething
    on reference that has something. E.g. Referencing an item on a function that does not have any value will cause this error.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

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

    This code:

    Code:
    RichTextBox1.SelectedText = GetWordWithOutBracketedText(kamau)
    was positioned here:

    Code:
     Private Sub ContextMenuStrip1_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
    
    RichTextBox1.SelectedText = GetWordWithOutBracketedText(kamau)
    
    End Sub
    After conducting a research on what causes these errors, I moved it to another location where the function was not null.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

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

    Referencing an object that is Nothing, or an item of such an object, is what causes the error. A function can't be Nothing, and functions don't have items, so I'm not quite sure what you were trying to say. This is perhaps the single most common exception encountered in .NET, and one of the easiest to solve. You find the line that threw the exception, and find the object that is Nothing. In some rare cases, you get taken to the wrong line, in which case it's harder to find the object that is Nothing, but it's still what has to be done. Usually, once you find the object that is Nothing, it's clear why it is Nothing, which suggests how to fix the problem. Sometimes, the object SHOULD be Nothing, and the solution is just to check for that. Other times, the object shouldn't be Nothing, and that is what has to be fixed.
    My usual boring signature: Nothing

Tags for this Thread

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