dotnet / dotnet/maui

SourceGen NullReferenceException

Open
#33,709 12 comments 5 reactions 1 assignee Claimed by @simonrozsival View on GitHub
i/regression regressed-in-10-rc2 s/triaged s/verified t/bug xsg
Dominant language
C#
Stars
23.3k
Forks
2k
Avg merge
1d 15h
Merged PRs (30d)
290

Description

### Description

When you have a binding

`Property="{Binding ViewModelProperty.Property}"`

and

`SourceGen`

it will generate

```
static global::Microsoft.Maui.Controls.BindingBase CreateTypedBindingFrom_bindingExtension94(global::Microsoft.Maui.Controls.Xaml.BindingExtension extension)
{
global::System.Action? setter = static (source, value) =>
{
source.ViewModelProperty.Property = value; // NullReferenceException is here because ViewModelProperty is null and it is ok for it to be null when data loads after the page appears. It doesn't make sense to create a Dummy object for every property that is set after setting binding context
};

return new global::Microsoft.Maui.Controls.Internals.TypedBinding(
getter: source => (source.ViewModelProperty.Property, true),
setter,
handlers: new global::System.Tuple, string>[]
{
new(static source => source, "ViewModelProperty"),
new(static source => source.ViewModelProperty, "Property"),
});
}
```
So every time you launch the app you must tap continue on those exceptions that pop up which will be swallowed and will not crash the app but it is very annoying.

This should be handled in some normal way where null on ViewModelProperty is just ignored because it is a valid case for properties on VM. Not everything can be loaded before binding context is set

### Steps to Reproduce

1. Bind to control property `Property="{Binding ViewModelProperty.Property}"` while ViewModelProperty is some object and initially null.
2. Turn on `SourceGen`
3. Launch the app and navigate to the page with the binding

Expected: Null is ignored, no NullReferenceException is thrown even if it is swallowed/silenced
Actual: NullReferenceException is thrown as if null is not an expected valid value.

### Link to public reproduction project repository

_No response_

### Version with bug

10.0.30

### Is this a regression from previous behavior?

Not sure, did not test other versions

### Last version that worked well

Unknown/Other

### Affected platforms

iOS, Android, I was *not* able test on other platforms

### Affected platform versions

_No response_

### Did you find any workaround?

_No response_

### Relevant log output

```shell

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.