dotnet/wpf · error · ArgumentNullException
queue
Error message
queue
What it means
The PrintContext constructor throws ArgumentNullException with parameter name "queue" when a null PrintQueue is passed. A print context is meaningless without a target print queue — it is used to open the print document stream (StartDoc/EndDoc), so the queue is a mandatory dependency.
Solutions
- Validate the PrintQueue before constructing PrintContext and surface a user-facing 'select a printer' message
- Resolve the queue explicitly: new PrintContext(GetValidQueue()) and fail early with a clear error if null
- Handle the case where the selected printer no longer exists by re-enumerating LocalPrintServer.GetPrintQueues
Example fix
// before
var ctx = new PrintContext(dialog.PrintQueue); // null if user never picked a printer
// after
if (dialog.PrintQueue == null) { ShowSelectPrinterDialog(); return; }
var ctx = new PrintContext(dialog.PrintQueue); Defensive patterns
Strategy: validation
Validate before calling
if (queue == null)
throw new InvalidOperationException("A print queue must be selected before printing."); Try / catch
try { var ctx = new PrintContext(queue); }
catch (ArgumentNullException ex) when (ex.ParamName == "queue") { ShowSelectPrinterDialog(); } Prevention
- Validate printer selection in UI before constructing PrintContext
- Re-resolve the queue from LocalPrintServer if the saved printer name no longer exists
- Fail early with a clear 'no printer selected' message rather than at construction
When it happens
Trigger: new PrintContext(null) — typically when the PrintQueue came from a failed lookup (LocalPrintServer.GetPrintQueue returning null), an unresolved variable, or a PrintDialog whose PrintQueue was never assigned.
Common situations: Print-queue selection UI where no printer was chosen; print-server enumeration returning a missing queue; deserializing saved print settings referencing a removed printer.
Related errors
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/2af144589db48b2b.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/printcontext.cs:186
}
}
}
#region public class PrintContext
/// <summary>
/// PrintContext provides visual level printing API
/// </summary>
public class PrintContext : DispatcherObject
{
/// <summary>
/// Connect to a specified print queue
/// </summary>
/// <param name="queue"></param>
public PrintContext(PrintQueue queue)
{
if( queue == null )
{
throw new ArgumentNullException("queue");
}
// enter batching for printing
MediaContext.CurrentMediaContext.ChannelSyncMode = true;
_queue = queue;
_jobTicket = new JobTicket(queue.UserJobTicket); // Make a new copy
}
#region Print()
/// <summary>
/// Print a job
/// </summary>
public void Print(GetPageCallback getPage, string jobName)
{
try
{
if (getPage == null)View on GitHub (pinned to 81131a70a4)