AvaloniaUI/Avalonia · error · ArgumentNullException
geometryContext
Error message
geometryContext
What it means
Thrown by the PathMarkupParser constructor when the IGeometryContext argument is null. The parser is a writer: it parses a path markup string and emits BeginFigure/LineTo/BezierTo/etc. calls into the supplied IGeometryContext, so it cannot operate without a valid sink.
Source
Thrown at src/Avalonia.Base/Media/PathMarkupParser.cs:47
};
private IGeometryContext? _geometryContext;
private Point _currentPoint;
private Point? _beginFigurePoint;
private Point? _previousControlPoint;
private bool _isOpen;
private bool _isDisposed;
/// <summary>
/// Initializes a new instance of the <see cref="PathMarkupParser"/> class.
/// </summary>
/// <param name="geometryContext">The geometry context.</param>
/// <exception cref="ArgumentNullException">geometryContext</exception>
public PathMarkupParser(IGeometryContext geometryContext)
{
if (geometryContext == null)
{
throw new ArgumentNullException(nameof(geometryContext));
}
_geometryContext = geometryContext;
}
private enum Command
{
None,
FillRule,
Move,
Line,
HorizontalLine,
VerticalLine,
CubicBezierCurve,
QuadraticBezierCurve,
SmoothCubicBezierCurve,
SmoothQuadraticBezierCurve,
Arc,View on GitHub (pinned to 11c5427268)
Solutions
- Pass a non-null IGeometryContext, typically obtained via ((StreamGeometry)geometry).Open() or StreamGeometryContext from a PathGeometry.
- Guard the call site: if (context is not null) { using var p = new PathMarkupParser(context); p.Parse(data); }
- If the context comes from a factory/field, ensure it is assigned before parser construction and never set back to null.
Example fix
// before
var parser = new PathMarkupParser(null);
parser.Parse("M0,0 L10,10");
// after
var sg = new StreamGeometry();
using (var ctx = sg.Open())
using (var parser = new PathMarkupParser(ctx))
{
parser.Parse("M0,0 L10,10");
} Defensive patterns
Strategy: validation
Validate before calling
if (geometryContext is null)
throw new ArgumentNullException(nameof(geometryContext));
var parser = new PathMarkupParser(geometryContext); Type guard
static bool IsValidContext(IGeometryContext? c) => c is not null;
Prevention
- Obtain the context from StreamGeometry.Open() in the same scope that constructs the parser.
- Treat a null context as a programming error at the call site rather than catching it downstream.
When it happens
Trigger: Constructing the parser with a null context: new PathMarkupParser(null). Also happens when the variable holding the context was never assigned (e.g. a field left null, or a StreamGeometry.Open() result captured before the geometry was opened).
Common situations: Passing an unopened or disposed StreamGeometry's context, or wiring PathMarkupParser against a field that is conditionally initialized and happened to be null on this code path. Common when refactoring geometry creation or sharing a context across parsers.
Related errors
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/a2019f436439ba16.
Report an issue: GitHub.