none
"Object reference not set to an instance of an object." when selecting text on richtextbox

    Question

  • Hello everyone! I have been getting some nullexceptions on the following lines of code...:

                CommandFontBold.Checked = richtext.SelectionFont.Bold
                CommandFontItalic.Checked = richText.SelectionFont.Italic
                CommandFontUnderline.Checked = richText.SelectionFont.Underline
                CommandFontStrike.Checked = richText.SelectionFont.Strikeout
    ************** Exception Text **************
    System.NullReferenceException: Object reference not set to an instance of an object.
       at 78.frmDocument.UpdateSelectionCommandsState()
       at 87.frmDocument.TextSelectionChanged(Object sender, EventArgs e)
       at System.Windows.Forms.RichTextBox.OnSelectionChanged(EventArgs e)
       at System.Windows.Forms.RichTextBox.WmSelectionChange(SELCHANGE selChange)
       at System.Windows.Forms.RichTextBox.WmReflectNotify(Message& m)
       at System.Windows.Forms.RichTextBox.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


    Its a text editor that i am working on. When a text is bolded and i have selected it, the button for bolding text becomes checked and same goes to the other ones like italic,underline and strikeout.

    but if I have selected a text with bold and italics and multiple ones, it gives me a "Object reference not set to an instance of an object." How could i solve this?

    I could provide a picture if needed 

    Thanks




    Saturday, April 14, 2012 4:35 PM

All replies

  • May be that when you select multiple font types , there isn't any returned.

    Use an IF to check for that

    If Not( richtext.SelectionFont Is Nothing) Then

    '''Your code

    End if



    Muscalu Ștefan ~~~~ Student


    Saturday, April 14, 2012 4:46 PM
  • Check if the SelectionFont is Nothing? According to thehelp, it will be Nothing if more than one font applies to the selection (and I take that to mean bold and italic count as separate fonts).

    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    Saturday, April 14, 2012 4:51 PM
  • Thanks for your answer. I have tried it and so far it hasn't been giving me any errors but now one last problem, everything I select the text again, it automaticly converts it to size 13 font? This code has also been giving me object errors.
    CommandFontSize.Text = (CInt(Fix(rtfText.SelectionFont.Size))).ToString()

    Saturday, April 14, 2012 5:07 PM
  • Isn't this the same problem? I.e. you need to check that the font is not nothing because it might have multiple styles?

    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    Saturday, April 14, 2012 5:15 PM
  • Can you present the full code in the selection event please?


    Muscalu Ștefan ~~~~ Student

    Saturday, April 14, 2012 5:16 PM
  • Yea I have fixed the object errors in the commandfontsize thing but it still oddly makes every text i select into size 13
    Saturday, April 14, 2012 5:17 PM
  • Can you present the full code in the selection event please?


    Muscalu Ștefan ~~~~ Student

     Private Sub Updee()
    
               
              
                    CommandFontSize.Text = (CInt(Fix(rtfText.SelectionFont.Size))).ToString
                If Not (rtfText.SelectionFont Is Nothing) Then
                    CommandFontBold.Checked = rtfText.SelectionFont.Bold()
                    CommandFontItalic.Checked = rtfText.SelectionFont.Italic
                    CommandFontUnderline.Checked = rtfText.SelectionFont.Underline
                    CommandFontStrike.Checked = rtfText.SelectionFont.Strikeout
    
                End If
                If rtfText.SelectionFont IsNot Nothing Then
                    CommandFont.Text = rtfText.SelectionFont.Name
    
                Else
                    CommandFont.Text = ""
                End If
            '    CommandFontSize.Text = (CInt(Fix(rtfText.SelectionFont.Size))).ToString()
                CommandAlignLeft.Checked = (rtfText.SelectionAlignment = HorizontalAlignment.Left)
                CommandAlignRight.Checked = (rtfText.SelectionAlignment = HorizontalAlignment.Right)
                CommandAlignCenter.Checked = (rtfText.SelectionAlignment = HorizontalAlignment.Center)
            End Sub


    Saturday, April 14, 2012 5:21 PM
  • Can you present the full code in the selection event please?



    Muscalu Ștefan ~~~~ Student

     Private Sub Updee()
    
               
              
                    CommandFontSize.Text = (CInt(Fix(rtfText.SelectionFont.Size))).ToString
                If Not (rtfText.SelectionFont Is Nothing) Then
                    CommandFontBold.Checked = rtfText.SelectionFont.Bold()
                    CommandFontItalic.Checked = rtfText.SelectionFont.Italic
                    CommandFontUnderline.Checked = rtfText.SelectionFont.Underline
                    CommandFontStrike.Checked = rtfText.SelectionFont.Strikeout
    
                End If
                If rtfText.SelectionFont IsNot Nothing Then
                    CommandFont.Text = rtfText.SelectionFont.Name
    
                Else
                    CommandFont.Text = ""
                End If
            '    CommandFontSize.Text = (CInt(Fix(rtfText.SelectionFont.Size))).ToString()
                CommandAlignLeft.Checked = (rtfText.SelectionAlignment = HorizontalAlignment.Left)
                CommandAlignRight.Checked = (rtfText.SelectionAlignment = HorizontalAlignment.Right)
                CommandAlignCenter.Checked = (rtfText.SelectionAlignment = HorizontalAlignment.Center)
            End Sub


    CommandFontSize.Text = (CInt(Fix(rtfText.SelectionFont.Size))).ToString ' should also be in the If statement

    Also, the first if statement and the second one are the same thing:

         If Not (rtfText.SelectionFont Is Nothing) is the same thing as If rtfText.SelectionFont IsNot Nothing

    There isn't any code in here that would change the selected font's properties. is there any other code in the SelectionChanged event?



    Muscalu Ștefan ~~~~ Student



    Saturday, April 14, 2012 5:31 PM
  • Updee()
    			Dim combo As ComboBoxItem = TryCast(sender, ComboBoxItem)
    			If combo IsNot Nothing AndAlso combo.SelectedItem IsNot Nothing Then
    				Dim font As New Font(rtfText.SelectionFont.FontFamily, Integer.Parse(combo.SelectedItem.ToString()))
    				rtfText.SelectionFont = font
    			End If
    			CommandExecuted()
    		End Sub
    Nope there isn't any other code in the selectionchanged

    Saturday, April 14, 2012 5:37 PM
  • Updee()
    			Dim combo As ComboBoxItem = TryCast(sender, ComboBoxItem)
    			If combo IsNot Nothing AndAlso combo.SelectedItem IsNot Nothing Then
    				Dim font As New Font(rtfText.SelectionFont.FontFamily, Integer.Parse(combo.SelectedItem.ToString()))
    				rtfText.SelectionFont = font
    			End If
    			CommandExecuted()
    		End Sub
    Nope there isn't any other code in the selectionchanged

    If combo IsNot Nothing AndAlso combo.SelectedItem IsNot Nothing Then ' should be

    If combo IsNot Nothing AndAlso combo.SelectedItem IsNot Nothing AndAlso rtfText.SelectionFont IsNot Nothing Then

    because you use  rtfText.SelectionFont.FontFamily

    I can't see the problem why it's changing automaticaly...



    Muscalu Ștefan ~~~~ Student





    Saturday, April 14, 2012 5:52 PM
  • Thanks :D It fixed part of the issue but im still confused because I have never set combo.selecteditem to 13 or anything else. When I start the application, it automatically chooses 8 as font size.
    Saturday, April 14, 2012 6:07 PM
  • If it's ok with you, can you send me via http://ge.tt/ the source file to look deeper into it?


    Muscalu Ștefan ~~~~ Student

    Saturday, April 14, 2012 6:14 PM
  • Ok heres the link 

    removed


    Saturday, April 14, 2012 6:19 PM
  • when does the size changes automatically ?

    The only possible way (in the code sent) that you can change size is at CommandFontSize_Executed

    In which circumstance does [Command].Executed is raised?

    I also noticed that you transform a "Command" type into a "ComboBox" at CommandFontSize_Executed. That may/will produce an error. I can't run your code because you have external library's that i don't have and when i try to run your exe, the process starts, the exits without UI.



    Muscalu Ștefan ~~~~ Student


    Saturday, April 14, 2012 6:41 PM
  • Should i resend you the source with the external librays then?
    Saturday, April 14, 2012 6:43 PM
  • Should i resend you the source with the external librays then?
    Sure...


    Muscalu Ștefan ~~~~ Student

    Saturday, April 14, 2012 6:44 PM
  • I binded the commandfontsize in   Private Sub BindDocumentCommands()

    making combofont in the UI into commandfontsize.

    If you can't get the interface loading just import the dotnetbar2.dll in the project

    Saturday, April 14, 2012 6:48 PM
  • Look's like when you select text with multiple seizes, the Richtextbox control interprets it as size 13 and that rises the font size change in CommandFontSize that actually changes the font size. :|

    That is weird, because a font with different sizes should be 2 different font values and the richtextbox should return nothing, but it return's the common cont family with size 13.

    This happens when text with the same font but different sizes is selected



    Muscalu Ștefan ~~~~ Student




    Saturday, April 14, 2012 7:11 PM
  • Oh is there a reason why the richtextbox control interprets it as 13? is there any way to fix it 
    Saturday, April 14, 2012 7:16 PM
  • "Object reference not set to an instance of an object." when selecting text on richtextbox.....

    Did you declare any Subs, variables or controls as Shared?

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/


    • Edited by Rudedog2MVP Saturday, April 14, 2012 7:31 PM
    Saturday, April 14, 2012 7:27 PM
  • Oh is there a reason why the richtextbox control interprets it as 13? is there any way to fix it 

    I don't this is something that you can fix.

    here's an example http://ge.tt/9CQ5aKG

    Select the different fonts and you'll get a message of "Multiple selected" in the text of the form

    Select the same font, different size and you'll get that font and size 13 in the text of the form

    The only thing that i can think of that you can do is to put a statement to cancel the change

    Add this   If rtfText.SelectionFont.Size = 13 Then Exit Sub   Just below   If combo IsNot Nothing AndAlso combo.SelectedItem IsNot Nothing AndAlso rtfText.SelectionFont IsNot Nothing Then

    This may be a bug in Richtextbox, you can open a new thread regarding this bug, and get help from people that know more than i do



    Muscalu Ștefan ~~~~ Student





    Saturday, April 14, 2012 7:31 PM
  • No i meant is there anyway to stop the combofont from changing to size 13 even though it interprets it as 13?

    In your example i selected the same fonts but diffrent sizes and they do not change to 13 and they both remain the same size

    Oh btw thanks for helping me a lot :D sorry if im wasting your time or something
    Saturday, April 14, 2012 7:41 PM
  • No i meant is there anyway to stop the combofont from changing to size 13 even though it interprets it as 13?

    In your example i selected the same fonts but diffrent sizes and they do not change to 13 and they both remain the same size

    Oh btw thanks for helping me a lot :D sorry if im wasting your time or something

    Sorry, but i have no idea why. I helped you to determine the source of the problem, now is the turn of the experts to tell you why that happens.

    BTW, no problem, i like to help when i can, where i can and how i can.

    BTW2, your source code is safe with me.(already deleted it :D)



    Muscalu Ștefan ~~~~ Student




    Saturday, April 14, 2012 7:44 PM
  • hi darkside,

    How about your issue going?

    It seems that you doesn't solve your issue. would you like to post the information about it?



    No code, No fact.


    • Edited by calanghei Tuesday, April 17, 2012 5:09 AM
    Tuesday, April 17, 2012 5:07 AM