System.NullReferenceException at Window.Close() from Opened or Activated handler
See original GitHub issueWhen 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:
- Created 2 years ago
- Comments:8 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
OnActivated
andOnOpened
should only be raised when the Window is initialized so this is a bugOh yeah…the stack trace 😃
Exception source: Avalonia.Controls