Tichau/FileConverter · error · Exception

The conversion preset must be valid.

Error message

The conversion preset must be valid.

What it means

ConversionJob_Excel.Convert() is a defensive invariant guard: it throws System.Exception when this.ConversionPreset is null before driving the NetOffice Excel interop (load workbook, export intermediate PDF, then pdf2Image sub-job). The real constructor ConversionJob(ConversionPreset, string) at ConversionJob.cs:42 already rejects a null preset with ArgumentNullException, so under the public API this guard is only reachable through the parameterless ConversionJob_Excel() : base() design-mode constructor (ConversionJob.cs:26) which deliberately sets ConversionPreset = null. Hitting it therefore signals misuse (wrong constructor, reflection, or a unit test calling protected Convert() directly) rather than a normal runtime failure.

Source

Thrown at Application/FileConverter/ConversionJobs/ConversionJob_Excel.cs:94

            }
            else
            {
                // Generate intermediate file path.
                string fileName = Path.GetFileNameWithoutExtension(this.InputFilePath);
                string tempPath = Path.GetTempPath();
                this.intermediateFilePath = PathHelpers.GenerateUniquePath(tempPath + fileName + ".pdf");

                ConversionPreset intermediatePreset = new ConversionPreset("Pdf to image", this.ConversionPreset, "pdf");
                this.pdf2ImageConversionJob = ConversionJobFactory.Create(intermediatePreset, this.intermediateFilePath);
                this.pdf2ImageConversionJob.PrepareConversion(this.OutputFilePaths);
            }
        }

        protected override void Convert()
        {
            if (this.ConversionPreset == null)
            {
                throw new System.Exception("The conversion preset must be valid.");
            }

            this.UserState = Properties.Resources.ConversionStateReadDocument;

            if (!this.TryLoadDocumentIfNecessary())
            {
                this.ConversionFailed(Properties.Resources.ErrorUnableToUseMicrosoftOffice);
                return;
            }

            // Make this document the active document.
            this.document.Activate();

            this.UserState = Properties.Resources.ConversionStateConversion;

            Debug.Log("Convert excel document to pdf.");
            this.document.ExportAsFixedFormat(Excel.Enums.XlFixedFormatType.xlTypePDF, this.intermediateFilePath);

View on GitHub (pinned to 6c157a411f)

Solutions

  1. Construct via the real ctor: new ConversionJob_Excel(conversionPreset, inputFilePath) — it throws ArgumentNullException early if the preset is null.
  2. Never call the protected Convert() directly; drive the job through PrepareConversion() then StartConversion(), which enforce the Ready state.
  3. If you must use the parameterless ctor (design time), gate it with if (ConversionPreset == null) return; before any conversion logic.
  4. Audit callers to confirm the preset was loaded successfully from Settings.user.xml before the job is created.

Example fix

// before (design-mode ctor, then convert)
var job = new ConversionJob_Excel();
job.Convert(); // throws: preset null

// after
var job = new ConversionJob_Excel(conversionPreset, inputFilePath);
job.PrepareConversion(outputPaths);
job.StartConversion();
Defensive patterns

Strategy: validation

Validate before calling

// Run before StartConversion / Convert
if (job == null || job.ConversionPreset == null)
{
    throw new InvalidOperationException(
        "ConversionJob_Excel requires a non-null ConversionPreset; use the (preset, inputFilePath) constructor.");
}
job.StartConversion();

Type guard

private static bool HasValidPreset(ConversionJob job) => job != null && job.ConversionPreset != null;

Try / catch

try
{
    job.StartConversion();
}
catch (Exception ex) when (ex.Message.Contains("conversion preset must be valid"))
{
    // Preset invariant violated: rebuild the job with a loaded preset, or fail the batch entry gracefully.
    logger.Error(ex, "Excel job created without a preset; reconstructing.");
    job = new ConversionJob_Excel(LoadPreset(), inputPath);
}

Prevention

When it happens

Trigger: An instance built via the parameterless ConversionJob_Excel() (XAML designer / 'Design Mode') has Convert() invoked; or reflection/dynamic dispatch bypasses the (preset, inputFilePath) constructor, leaving ConversionPreset null when Convert() runs. StartConversion() itself would normally throw 'Invalid conversion state' first because the design-mode ctor sets State=InProgress, so direct Convert() invocation is the realistic path.

Common situations: XAML designer previewing a ConversionJob_Excel instance; a unit test that new's the parameterless ctor and calls Convert(); a future subclass that adds a constructor without chaining the preset; Office interop code that constructed the job before a preset was loaded from Settings.

Related errors


AI-assisted analysis of Tichau/FileConverter@6c157a411f (2026-08-13). Data as JSON: /api/errors/e38fd9f4ec8caa04. Report an issue: GitHub.