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.

NullReferenceException without explicit font specification

See original GitHub issue

Running my app on Linux Mate on a Raspberry gives me the following error

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at Avalonia.Skia.TypefaceCache.GetTypeface(String name, FontKey key)
   at Avalonia.Skia.FormattedTextImpl..ctor(String text, Typeface typeface, TextAlignment textAlignment, TextWrapping wrapping, Size constraint, IReadOnlyList`1 spans)
   at Avalonia.Skia.PlatformRenderInterface.CreateFormattedText(String text, Typeface typeface, TextAlignment textAlignment, TextWrapping wrapping, Size constraint, IReadOnlyList`1 spans)
   at Avalonia.Controls.TextBlock.MeasureOverride(Size availableSize)
   at Avalonia.Layout.Layoutable.MeasureCore(Size availableSize)
   at Avalonia.Layout.Layoutable.Measure(Size availableSize)
   at Avalonia.Controls.Grid.<>c__DisplayClass25_0.<MeasureOverride>g__MeasureOnce|2(Control child, Size size)
   at Avalonia.Controls.Grid.<>c__DisplayClass25_0.<MeasureOverride>b__0(Control child)
   at Avalonia.Controls.Utils.GridLayout.AppendMeasureConventions[T](IDictionary`2 source, Func`2 getDesiredLength)
   at Avalonia.Controls.Grid.MeasureOverride(Size constraint)
   at Avalonia.Layout.Layoutable.MeasureCore(Size availableSize)
   at Avalonia.Layout.Layoutable.Measure(Size availableSize)
   at Avalonia.Controls.Grid.<>c__DisplayClass25_0.<MeasureOverride>g__MeasureOnce|2(Control child, Size size)
   at Avalonia.Controls.Grid.<>c__DisplayClass25_0.<MeasureOverride>b__0(Control child)
   at Avalonia.Controls.Utils.GridLayout.AppendMeasureConventions[T](IDictionary`2 source, Func`2 getDesiredLength)
   at Avalonia.Controls.Grid.MeasureOverride(Size constraint)
   at Avalonia.Layout.Layoutable.MeasureCore(Size availableSize)
   at Avalonia.Layout.Layoutable.Measure(Size availableSize)
   at Avalonia.Layout.LayoutHelper.MeasureChild(ILayoutable control, Size availableSize, Thickness padding)
   at Avalonia.Controls.Presenters.ContentPresenter.MeasureOverride(Size availableSize)
   at Avalonia.Layout.Layoutable.MeasureCore(Size availableSize)
   at Avalonia.Layout.Layoutable.Measure(Size availableSize)
   at Avalonia.Layout.LayoutHelper.MeasureChild(ILayoutable control, Size availableSize, Thickness padding)
   at Avalonia.Controls.Primitives.AdornerDecorator.MeasureOverride(Size availableSize)
   at Avalonia.Layout.Layoutable.MeasureCore(Size availableSize)
   at Avalonia.Layout.Layoutable.Measure(Size availableSize)
   at Avalonia.Layout.LayoutHelper.MeasureChild(ILayoutable control, Size availableSize, Thickness padding)
   at Avalonia.Controls.Border.MeasureOverride(Size availableSize)
   at Avalonia.Layout.Layoutable.MeasureCore(Size availableSize)
   at Avalonia.Layout.Layoutable.Measure(Size availableSize)
   at Avalonia.Layout.Layoutable.MeasureOverride(Size availableSize)
   at Avalonia.Controls.Window.MeasureOverride(Size availableSize)
   at Avalonia.Layout.Layoutable.MeasureCore(Size availableSize)
   at Avalonia.Layout.Layoutable.Measure(Size availableSize)
   at Avalonia.Layout.LayoutManager.Measure(ILayoutable control)
   at Avalonia.Layout.LayoutManager.ExecuteInitialLayoutPass(ILayoutRoot root)
   at Avalonia.Controls.Window.Show()
   at Avalonia.Application.Run(Window mainWindow)
   at Avalonia.Controls.AppBuilderBase`1.Start[TMainWindow](Func`1 dataContextProvider)
   at MyApp.Program.Main(String[] args) in C:\MyApp\Program.cs:line 11
Aborted

The error is resolved by setting the FontFamily property on my main window. In this case I have to set it to Ubuntu. However, when I run the app on Linux Raspian it fails again with the same error. Setting the FontFamily to Piboto solves it. Actually all I want is that the app uses the default desktop font, whatever that is, without specifying it explicitly. Is this is problem in Skia or Avalonia or are we as application developers to required to fulfill any prerequisites?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:21 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
Gillibaldcommented, Sep 28, 2018

Will change the logic to use SKTypeface.FromFamilyName(null) when null is returned.

@mattleibow Thanks for making this clear.

1reaction
mattleibowcommented, Sep 28, 2018

I had a look and it appears that in some cases, the fontconfig font manager will return null. I don’t think there was ever a guarantee that if you request a font it would return a system default.

See this line: https://github.com/google/skia/blob/chrome/m68/src/ports/SkFontMgr_fontconfig.cpp#L894

In the next (m68) release, I added a SKTypeface.Default property that can be used and should never be null as per the comment in the source: https://github.com/google/skia/blob/chrome/m68/include/core/SkTypeface.h#L90-L91

In the meantime, a fallback is to pass null to SKTypeface.FromFamilyName(null). The source for that reaches here: https://github.com/google/skia/blob/chrome/m68/src/core/SkTypeface.cpp#L135-L144 and the GetDefaultTypeface reference is the same used in SkTypeface::MakeDefault(): https://github.com/google/skia/blob/chrome/m68/src/core/SkTypeface.cpp#L97-L112

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - What is a NullReferenceException, and how do I fix it?
Introduced in C# 8 , null contexts and nullable reference types perform static analysis on variables and provide a compiler warning if a...
Read more >
NullReferenceException in DevExpress.Utils.Text. ...
Hi! Sometimes, I get the exception below (not easy to reproduce). I found other threads saying this was related to improper use from...
Read more >
NullReferenceException in C#. What is it and how to fix it?
A NullReferenceException (NRE) is a type of .NET exception. It occurs when a developer tries to dereference a null reference.
Read more >
NullReferenceException should not be caught
Any situation in which NullReferenceException is explicitly caught can easily be converted to a null test, and any behavior being carried out in...
Read more >
NullReferenceException in DevExpress.Utils.Text. ...
Hello,. We just had an exception raised with one of our clients which seems to be a problem in the DevX code (NullReference)....
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