﻿# Do you plan to take on \.NET MAUI? Get ready for an adventure with NullReferenceException

\.NET Multi\-platform App UI is a framework written by professionals\. However, the code of some of its functions looks like the developers could have forgotten what null reference dereferencing could lead to\.

![0996_NRE_MAUI/image1.png](https://import.viva64.com/docx/blog/0996_NRE_MAUI/image1.png)

## A brief introduction

I can't judge the overall quality of the MAUI project, but I suspect developers will spend quite a bit of time addressing all user reports about unexpected _NullReferenceException_\.

The authors may have been in a hurry to write the code\. As a result, some code fragments contain references, which, according to the developers, may be null, and which are not checked for null before dereferencing\. To make it clearer, let's take a look at some fragments from the MAUI 6\.0\.424 [source code](https://github.com/dotnet/maui/releases/tag/6.0.424)\. 

## Why so?

When I looked at the fragments I'm listing below, I asked myself the same question\. Either I'm missing something, or MAUI developers write very strange code\.

Let's take a look at the fragment from the _AttachEvents_ function of the _AdaptiveTrigger_ class:

```cpp
void AttachEvents()
{
  ....

  _visualElement = VisualState?.VisualStateGroup?.VisualElement;
  if (_visualElement is not null)
    _visualElement.PropertyChanged += OnVisualElementPropertyChanged;

  _window = _visualElement.Window;  // <=
  ....
}
```

It doesn't make any sense\. Judging by the two "?\." and the "is not null" check, we may expect _null_ in the _\_visualElement_ field\. Then the code accesses the _Window_ property, but without first checking _\_visualElement _for _null_\. Apparently, someone overlooked it\.

Okay, let's move on\. Take a look at the fragment from the _FormattedString_ class:

```cpp
void OnCollectionChanged(....)
{
  ....
    foreach (object item in e.OldItems)
    {
      var bo = item as Span;
      bo.Parent = null;                   // <=
      if (bo != null)
      {
        ....
      }
  .... 
}
```

The adjacent lines again\. First the developers dereferenced _bo_, and then suddenly decided to check it for _null_\. I'm not sure whether this code crashes in some cases, but it all looks strange\.

Here is a snippet from the _ListView_ class:

```cpp
protected override void SetupContent(Cell content, int index)
{
  ....

  if (content != null)
    _logicalChildren.Add(content);

  content.Parent = this;
  VisualDiagnostics.OnChildAdded(this, content);
}
```

It's all nonsense again\. Why do they perform a _null_ check only when adding an element to the collection, but skip the check when accessing the property?

I found another "useful" _null_ check in the constructor of the _ShellChromeGallery_ class:

```cpp
AppShell AppShell => Application.Current.MainPage as AppShell;

public ShellChromeGallery()
{
  ....

  if (AppShell != null)
  {
    flyoutBehavior.SelectedIndex = ....;
    flyoutHeaderBehavior.SelectedIndex = ....;
  }
  else
  {
    flyoutBehavior.SelectedIndex = 1;
    flyoutHeaderBehavior.SelectedIndex = 0;
  }

  AppShell.FlyoutBackdrop = SolidColorBrush.Pink;  // <=
}
```

Here we go again: we check for _null_, set some default values if necessary, and then crash **immediately**\.

Considering how often I encountered strange fragments, I even began to think that checking for _null_ was not what it seemed\. Take, for example, Unity: the "\=\=" operator is overloaded and the _null_ check works there in a peculiar way intentionally\. However, I didn't see anything like that in MAUI\. 

## Why is it so anyway?

MAUI is written by professionals, but the project is very big and complex\. People make mistakes, and that's okay\. The strange thing is, the project contains such problems despite the fact that various automated tools can catch them\. It's not hard to guess that I used the PVS\-Studio static analyzer for C\# to find these fragments\. In my opinion, such tools would simplify the life of MAUI developers\. In any case, I wish them success with the development of the project\.