Bug Report T306361
Visible to All Users

NullReferenceException occurs when document content is selected using the mouse

created 9 years ago (modified 9 years ago)

Hi DevExpress

I have a project where I wish to inspect the contents of the selected text in the XtraRichEdit control.

Within the SelectionChanged event I get the selected text of a .docx document by calling the following

C#
var sel = richEditControl1.Document.Selection; var selectedText = richEditControl1.Document.GetText(sel);

If the user then selects a large amount of text by clicking, holding left mouse button and dragging down (or using the mouse wheel) over multiple pages we get the exception below. I created a simple project and can reproduce each time with a .docx document.

C#
An unhandled exception of type 'System.NullReferenceException' occurred in DevExpress.XtraRichEdit.v15.1.dll Additional information: Object reference not set to an instance of an object.    at DevExpress.XtraRichEdit.Commands.Internal.ExtendSelectionByRangesCommandBase.set_InitialBox(Box value)    at DevExpress.XtraRichEdit.Mouse.ContinueSelectionByRangesMouseHandlerState.ContinueSelection(MouseEventArgs e)    at DevExpress.XtraRichEdit.Mouse.ContinueSelectionByCharactersMouseHandlerState.ContinueSelection(MouseEventArgs e)    at DevExpress.XtraRichEdit.Mouse.ContinueSelectionByRangesMouseHandlerState.OnMouseWheel(MouseEventArgs e)    at DevExpress.XtraRichEdit.Mouse.RichEditMouseHandler.HandleMouseWheel(MouseEventArgs e)    at DevExpress.Utils.MouseHandler.OnMouseWheel(MouseEventArgs e)    at DevExpress.Services.Implementation.MouseHandlerService.OnMouseWheel(MouseEventArgs e)    at DevExpress.XtraRichEdit.Internal.InnerRichEditControl.OnMouseWheel(MouseEventArgs e)    at DevExpress.XtraRichEdit.RichEditControl.OnMouseWheelCore(MouseEventArgs e)    at DevExpress.XtraRichEdit.RichEditControl.OnMouseWheel(MouseEventArgs e)    at System.Windows.Forms.Control.WmMouseWheel(Message& m)    at System.Windows.Forms.Control.WndProc(Message& m)    at DevExpress.XtraRichEdit.RichEditControl.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.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)    at System.Windows.Forms.Application.Run(Form mainForm)    at WindowsFormsApplication3.Program.Main() in c:\users\....\documents\visual studio 2015\Projects\WindowsFormsApplication3\WindowsFormsApplication3\Program.cs:line 19    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)    at System.Threading.ThreadHelper.ThreadStart()
Show previous comments (4)
JK JK
James Kennedy 2 9 years ago

    Actually, I have managed to repeat with .RTF documents as well

    Maria Nikulina (DevExpress) 9 years ago

      Well, the exception can occur with this code if you click the TextBox since the TextBox range relates to a different SubDocument in this case. To avoid this exception, you can use the following condition:

      C#
      var sel = richEditControl1.Document.Selection; SubDocument subDocument = sel.BeginUpdateDocument(); if (subDocument is Document) { sel.EndUpdateDocument(subDocument); var selectedText = richEditControl1.Document.GetText(sel); }

      However, the call stack of this exception is different than the call stack you specified previously, and I was not able to reproduce your exception on my side. I've attached a video illustrating the behavior in my case.
      So, I'd like to ask you to provide us with the project, document or a video illustrating how to reproduce it. Perhaps, it's possible to reproduce this exception with the document used in our Demo.
      I'm looking forward to your clarification.

      Maria Nikulina (DevExpress) 9 years ago

        Thank you for the sample. I reproduced this behavior and passed the issue to our R&D team for further research.
        Please watch for our updates. We will notify you as soon as we make any progress.

        Answers approved by DevExpress Support

        created 9 years ago

        We have fixed the issue described in this ticket and will include the fix in our next maintenance update. To apply this solution before the official update, request a hotfix by clicking the corresponding link for product versions you require.

        Note: Hotfixes may be unavailable for beta versions and updates that are about to be released.

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.