question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

System.NullReferenceException at Window.Close() from Opened or Activated handler

See original GitHub issue

When calling Close() from inside an event handler for Opened or Activated, an uncaught System.NullReferenceException occurs in the Main() function of the program.

Expected behavior Window closes (and application terminates when using default shutdown mode).

Steps to reproduce Create an Avalonia Application and change the code in MainWindow.cs as follows:

  public class MainWindow : Window
  {
      public MainWindow()
      {
          Opened += OnOpened;
          // Uncomment the line above and comment the line above
          //Activated += OnActivated;
          InitializeComponent();
      }
  
      private void OnActivated(object? sender, EventArgs e)
      {
          Close(); // <= Causes an exception
      }
  
      private void OnOpened(object? sender, EventArgs e)
      {
          Close(); // <= Causes an exception
      }
  
      private void InitializeComponent()
      {
          AvaloniaXamlLoader.Load(this);
      }
  }

The exception occurs in Main():

  [STAThread]
  public static void Main(string[] args) => BuildAvaloniaApp()
           .StartWithClassicDesktopLifetime(args); // <= Uncaught System.NullReferenceException

Desktop

  • OS: Windows 10
  • Version 0.10.10 and 0.10.11

Additional context If you show a dialog window before calling Close(), the behavior is as expected:

  public class MainWindow : Window
  {
      public MainWindow()
      {
          Opened += OnOpened;
          InitializeComponent();
      }
  
      private async void OnOpened(object? sender, EventArgs e)
      {
          Window dialogWindow = new VersionUpgradeWindow();
          await dialogWindow.ShowDialog(this);
          Close(); // <== No exception, window and application closes
      }
  
      private void InitializeComponent()
      {
          AvaloniaXamlLoader.Load(this);
      }
  }

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Gillibaldcommented, Feb 22, 2022

OnActivated and OnOpened should only be raised when the Window is initialized so this is a bug

1reaction
Maikelsoftcommented, Dec 25, 2021

Oh yeah…the stack trace 😃

   at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.Start(String[] args) in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 132
   at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime[T](T builder, String[] args, ShutdownMode shutdownMode) in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 187
   at Maikelsoft.PhotoTool.AvaloniaUI.Program.Main(String[] args) in ************************\Program.cs:line 15

Exception source: Avalonia.Controls

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unhandled NullReference exception when closing WPF ...
An unhandled exception in a finalizer is fatal, they'll always terminate the program. ... Typically in the Window's Closing event handler.
Read more >
How can I fix the error: System.NullReferenceException
How can I fix the error: System.NullReferenceException: 'Object reference not set to an instance of an object.'.
Read more >
JavaScript Window close method
JavaScript provides an in-built function named close() to close the browser window that is opened by using window.open() method. Unlike the window.open() ...
Read more >
Window: close() method - Web APIs - MDN Web Docs - Mozilla
The Window.close() method closes the current window, or the window on which it was called. This method can only be called on windows...
Read more >
Studio - Global Exception Handler
The Global Exception Handler is a type of workflow designed to determine the project's behavior when encountering ... The New Global Handler window...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found