Avalonia.Utilities.WeakEvent: null reference exception
See original GitHub issueDescribe the bug
Sometimes I get NullReferenceException
when using Avalonia with ReactiveUI
.
Stacktraces:
System.NullReferenceException: Object reference not set to an instance of an object.
at Avalonia.Utilities.WeakEvent`2.Subscription.OnEvent(Object sender, TEventArgs eventArgs) in /_/src/Avalonia.Base/Utilities/WeakEvent.cs:line 162
at Avalonia.Utilities.WeakEvents.<>c__DisplayClass5_1.<.cctor>b__7(Object _, PropertyChangedEventArgs e) in /_/src/Avalonia.Base/Utilities/WeakEvents.cs:line 29
at ReactiveUI.ReactiveObject.ReactiveUI.IReactiveObject.RaisePropertyChanged(PropertyChangedEventArgs args) in /_/src/ReactiveUI/ReactiveObject/ReactiveObject.cs:line 96
System.NullReferenceException: Object reference not set to an instance of an object.
at Avalonia.Utilities.WeakHashList`1.GetAlive(Func`1 factory) in /_/src/Avalonia.Base/Utilities/WeakHashList.cs:line 210
at Avalonia.Utilities.WeakEvent`2.Subscription.OnEvent(Object sender, TEventArgs eventArgs) in /_/src/Avalonia.Base/Utilities/WeakEvent.cs:line 157
at Avalonia.Utilities.WeakEvents.<>c__DisplayClass5_1.<.cctor>b__7(Object _, PropertyChangedEventArgs e) in /_/src/Avalonia.Base/Utilities/WeakEvents.cs:line 29
at ReactiveUI.ReactiveObject.ReactiveUI.IReactiveObject.RaisePropertyChanged(PropertyChangedEventArgs args) in /_/src/ReactiveUI/ReactiveObject/ReactiveObject.cs:line 96
Full stacktrace
System.NullReferenceException: Object reference not set to an instance of an object.
at Avalonia.Utilities.WeakHashList`1.GetAlive(Func`1 factory) in /_/src/Avalonia.Base/Utilities/WeakHashList.cs:line 210
at Avalonia.Utilities.WeakEvent`2.Subscription.OnEvent(Object sender, TEventArgs eventArgs) in /_/src/Avalonia.Base/Utilities/WeakEvent.cs:line 157
at Avalonia.Utilities.WeakEvents.<>c__DisplayClass5_1.<.cctor>b__7(Object _, PropertyChangedEventArgs e) in /_/src/Avalonia.Base/Utilities/WeakEvents.cs:line 29
at ReactiveUI.ReactiveObject.ReactiveUI.IReactiveObject.RaisePropertyChanged(PropertyChangedEventArgs args) in /_/src/ReactiveUI/ReactiveObject/ReactiveObject.cs:line 96
at System.Reactive.AnonymousSafeObserver`1.OnNext(T value) in /_/Rx.NET/Source/src/System.Reactive/AnonymousSafeObserver.cs:line 43
at System.Reactive.Sink`1.ForwardOnNext(TTarget value) in /_/Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:line 49
at System.Reactive.Linq.ObservableImpl.SelectMany`2.EnumerableSelector._.OnNext(TSource value) in /_/Rx.NET/Source/src/System.Reactive/Linq/Observable/SelectMany.cs:line 1375
at System.Reactive.Sink`1.ForwardOnNext(TTarget value) in /_/Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:line 49
at System.Reactive.Linq.ObservableImpl.Buffer`2.Boundaries._.BufferClosingObserver.OnNext(TBufferClosing value) in /_/Rx.NET/Source/src/System.Reactive/Linq/Observable/Buffer.cs:line 827
at System.Reactive.Sink`1.ForwardOnNext(TTarget value) in /_/Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:line 49
at System.Reactive.Linq.ObservableImpl.Merge`1.Observables._.InnerObserver.OnNext(TSource value) in /_/Rx.NET/Source/src/System.Reactive/Linq/Observable/Merge.cs:line 238
at System.Reactive.Sink`1.ForwardOnNext(TTarget value) in /_/Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:line 49
at System.Reactive.Linq.ObservableImpl.Select`2.Selector._.OnNext(TSource value) in /_/Rx.NET/Source/src/System.Reactive/Linq/Observable/Select.cs:line 47
at System.Reactive.Sink`1.ForwardOnNext(TTarget value) in /_/Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:line 49
at System.Reactive.Linq.ObservableImpl.Where`1.Predicate._.OnNext(TSource value) in /_/Rx.NET/Source/src/System.Reactive/Linq/Observable/Where.cs:line 54
at System.Reactive.Subjects.Subject`1.OnNext(T value) in /_/Rx.NET/Source/src/System.Reactive/Subjects/Subject.cs:line 147
at ReactiveUI.IReactiveObjectExtensions.ExtensionState`1.RaisePropertyChanged(String propertyName) in /_/src/ReactiveUI/ReactiveObject/IReactiveObjectExtensions.cs:line 390
at ReactiveUI.IReactiveObjectExtensions.RaisingPropertyChanged[TSender](TSender reactiveObject, String propertyName) in /_/src/ReactiveUI/ReactiveObject/IReactiveObjectExtensions.cs:line 245
at ReactiveUI.IReactiveObjectExtensions.RaiseAndSetIfChanged[TObj,TRet](TObj reactiveObject, TRet& backingField, TRet newValue, String propertyName) in /_/src/ReactiveUI/ReactiveObject/IReactiveObjectExtensions.cs:line 131
at Avalonia.NETCoreMVVMApp2.ViewModels.TreeViewModel.set_IsLoading(Boolean value) in C:\Users\mitya\RiderProjects\Avalonia.NETCoreMVVMApp2\Avalonia.NETCoreMVVMApp2\ViewModels\MainWindowViewModel.cs:line 51
at Avalonia.NETCoreMVVMApp2.ViewModels.TreeViewModel.Populate(String arg) in C:\Users\mitya\RiderProjects\Avalonia.NETCoreMVVMApp2\Avalonia.NETCoreMVVMApp2\ViewModels\MainWindowViewModel.cs:line 64
at System.Reactive.PlatformServices.ExceptionServicesImpl.Rethrow(Exception exception) in /_/Rx.NET/Source/src/System.Reactive/Internal/ExceptionServicesImpl.cs:line 19
at System.Reactive.ExceptionHelpers.Throw(Exception exception) in /_/Rx.NET/Source/src/System.Reactive/Internal/ExceptionServices.cs:line 16
at System.Reactive.Stubs.<>c.<.cctor>b__2_1(Exception ex) in /_/Rx.NET/Source/src/System.Reactive/Internal/Stubs.cs:line 16
at System.Reactive.AnonymousSafeObserver`1.OnError(Exception error) in /_/Rx.NET/Source/src/System.Reactive/AnonymousSafeObserver.cs:line 62
at System.Reactive.Sink`1.ForwardOnError(Exception error) in /_/Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:line 60
at System.Reactive.ObserveOnObserverNew`1.DrainStep(ConcurrentQueue`1 q) in /_/Rx.NET/Source/src/System.Reactive/Internal/ScheduledObserver.cs:line 553
at System.Reactive.ObserveOnObserverNew`1.DrainShortRunning(IScheduler recursiveScheduler) in /_/Rx.NET/Source/src/System.Reactive/Internal/ScheduledObserver.cs:line 507
at System.Reactive.ObserveOnObserverNew`1.<>c.<.cctor>b__17_0(IScheduler scheduler, ObserveOnObserverNew`1 self) in /_/Rx.NET/Source/src/System.Reactive/Internal/ScheduledObserver.cs:line 497
at Avalonia.ReactiveUI.AvaloniaScheduler.<>c__DisplayClass4_1`1.<Schedule>b__1() in /_/src/Avalonia.ReactiveUI/AvaloniaScheduler.cs:line 48
at Avalonia.Threading.JobRunner.Job.Avalonia.Threading.JobRunner.IJob.Run() in /_/src/Avalonia.Base/Threading/JobRunner.cs:line 193
at Avalonia.Threading.JobRunner.RunJobs(Nullable`1 priority) in /_/src/Avalonia.Base/Threading/JobRunner.cs:line 38
at Avalonia.Win32.Win32Platform.WndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam) in /_/src/Windows/Avalonia.Win32/Win32Platform.cs:line 257
at Avalonia.Win32.Interop.UnmanagedMethods.DispatchMessage(MSG& lpmsg)
at Avalonia.Win32.Win32Platform.RunLoop(CancellationToken cancellationToken) in /_/src/Windows/Avalonia.Win32/Win32Platform.cs:line 204
at Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancellationToken) in /_/src/Avalonia.Base/Threading/Dispatcher.cs:line 61
at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.Start(String[] args) in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 120
at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime(AppBuilder builder, String[] args, ShutdownMode shutdownMode) in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 212
at Avalonia.NETCoreMVVMApp2.Program.Main(String[] args) in C:\Users\mitya\RiderProjects\Avalonia.NETCoreMVVMApp2\Avalonia.NETCoreMVVMApp2\Program.cs:line 17
I have two TreeView
controls with following viewmodels. After Task.Delay
in Populate
method is finished and IsLoading
is set to false
exception is thrown
public class TreeViewModel : ReactiveObject
{
public TreeViewModel()
{
this.WhenAnyValue(x => x.Filter)
.Throttle(TimeSpan.FromMilliseconds(800))
.Select(text => text.Trim())
.DistinctUntilChanged()
.SelectMany(Populate)
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe();
}
public ObservableCollection<NodeViewModel> Items { get; } = new();
[Reactive] public bool IsLoading { get; set; }
[Reactive] public string Filter { get; set; } = string.Empty;
private async Task<Unit> Populate(string arg)
{
try
{
IsLoading = true;
await Task.Delay(2000);
}
finally
{
IsLoading = false;
}
return Unit.Default;
}
}
<TextBlock Text="Loading..." IsVisible="{Binding Tree1.IsLoading}" />
<TreeView Items="{Binding Tree1.Items}" >
<TreeView.Styles>
<Style Selector="TreeViewItem" x:DataType="viewModels:NodeViewModel">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
</TreeView.Styles>
<TreeView.DataTemplates>
<TreeDataTemplate DataType="viewModels:NodeViewModel" ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Value}" />
</TreeDataTemplate>
</TreeView.DataTemplates>
</TreeView>
To Reproduce Steps to reproduce the behavior:
- Run attached repro project
- See the error (maybe you have to launch it several times)
Expected behavior No exception is thrown
Desktop (please complete the following information):
- OS: [Windows]
- Version [11-preview4, 11-preview5, .net6, .net7]
Additional context Version 0.10.18 worked without errors.
Issue Analytics
- State:
- Created 7 months ago
- Comments:12 (12 by maintainers)
Top Results From Across the Web
c# - Avalonia NullReferenceException when referencing ...
Why am I getting this null exception? This seems to work fine outside of Avalonia. I tried. Getting/setting in the UserControl (produced the ......
Read more >Messagebox with exception on close when no license entered
I experience the same issue after successful license activation: Enable vpn or connect to office internal wifi; Put these files to %localappdata ...
Read more >API - WeakEventHandlerManager Class - Avalonia UI
Manages subscriptions to events using weak listeners. Namespace: Avalonia.Utilities; Base Types. object.
Read more >uNmINeD - 2D mapper [JE & BE, Windows / Mac / Linux]
NullReferenceException : Object reference not set to an instance of an object. at Avalonia.Utilities.WeakHashList`1.GetAlive(Func`1 factory) ...
Read more >C# (CSharp) WeakEvent Examples
These are the top rated real world C# (CSharp) examples of WeakEvent ... public void OnEvent(object sender, WeakEvent ev, PropertyChangedEventArgs e) { if ......
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 FreeTop 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
Top GitHub Comments
In general it looks like a duplicate of this issue https://github.com/AvaloniaUI/Avalonia/issues/8810 With more details from @grokys .
Closing as a duplicate of https://github.com/AvaloniaUI/Avalonia/issues/8810