|
-
Mar 16th, 2017, 04:24 AM
#1
Thread Starter
Lively Member
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.
-
Mar 16th, 2017, 06:18 AM
#2
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?
-
Mar 16th, 2017, 06:21 AM
#3
Thread Starter
Lively Member
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.
-
Mar 16th, 2017, 06:24 AM
#4
Thread Starter
Lively Member
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.
-
Mar 16th, 2017, 12:26 PM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|