Bug Report T644192
Visible to All Users

WinForms - NullReferenceException occurs in some circumstances in the code of the Q559647 ticket

created 7 years ago (modified 7 years ago)

The NullReference exception occurs in some circumstances in the code of the How to set focus to a ParametrizedAction (Full Text Search) in code ticket:

> DXApplication2.Module.Win.dll!DXApplication2.Module.Win.Controllers.Controller1.FocusFullTextSearch(DevExpress.XtraBars.BarItem barItem) Line 90 C#
  DXApplication2.Module.Win.dll!DXApplication2.Module.Win.Controllers.Controller1.View_DocumentActivated(object sender, DevExpress.XtraBars.Docking2010.Views.DocumentEventArgs e) Line 76 C#
  DevExpress.XtraBars.v18.1.dll!DevExpress.XtraBars.Docking2010.Views.BaseView.RaiseDocumentActivated(DevExpress.XtraBars.Docking2010.Views.BaseDocument document) Unknown
  DevExpress.XtraBars.v18.1.dll!DevExpress.XtraBars.Docking2010.Views.BaseView.OnDocumentActivated(DevExpress.XtraBars.Docking2010.Views.BaseDocument document) Unknown
  DevExpress.XtraBars.v18.1.dll!DevExpress.XtraBars.Docking2010.Views.BaseView.SetActiveDocumentCore(DevExpress.XtraBars.Docking2010.Views.BaseDocument document) Unknown
  DevExpress.XtraBars.v18.1.dll!DevExpress.XtraBars.Docking2010.Views.BaseView.ActivateDocument(System.Windows.Forms.Control control) Unknown
  DevExpress.XtraBars.v18.1.dll!DevExpress.XtraBars.Docking2010.MDIClientManagementStrategy.OnMdiChildActivate(object sender, System.EventArgs e) Unknown

'Application.MainWindow' is null:

C#
private void FocusFullTextSearch(BarItem barItem) { ((Form)Application.MainWindow.Template).BeginInvoke(new MethodInvoker(() => { BarItemLinkReadOnlyCollection visibleLinks = barItem.GetVisibleLinks(); if (visibleLinks.Count > 0) { visibleLinks[0].Focus(); } })); }

Answers approved by DevExpress Support

created 6 years ago

Hello,
We researched this issue and found that Application.MainWindow can be null in the FocusFullTextSearch method when the application exits and UIType  is setto TabbedMDI. In this scenario, the FocusFullTextSearch  method is called from the TabbedView.DocumentActivated  event handler, which is raised when the Main Form closes. This situation occurs when the Form.Closed  event has been processed and the Application.MainWindow is already set to null. To avoid this exception, we recommend you check that the Application.MainWindow property is not null before changing focus.

C#
private void FocusFullTextSearch(BarItem barItem) { if(Application.MainWindow == null) { return; } ((Form)Application.MainWindow.Template).BeginInvoke(new MethodInvoker(() => { BarItemLinkReadOnlyCollection visibleLinks = barItem.GetVisibleLinks(); if(visibleLinks.Count > 0) { visibleLinks[0].Focus(); } })); }

We've updated the How to set focus to a ParametrizedAction (Full Text Search) in code ticket accordingly.

Thanks,
Nat.

    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.