{"record":{"id":"340e35a2d568e9e2","repo":"Tichau/FileConverter","slug":"the-conversion-preset-must-be-valid-340e35","errorCode":null,"errorMessage":"The conversion preset must be valid.","messagePattern":"The conversion preset must be valid\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Application/FileConverter/ConversionJobs/ConversionJob_Word.cs","lineNumber":86,"sourceCode":"            }\n            else\n            {\n                // Generate intermediate file path.\n                string fileName = Path.GetFileNameWithoutExtension(this.InputFilePath);\n                string tempPath = Path.GetTempPath();\n                this.intermediateFilePath = PathHelpers.GenerateUniquePath(tempPath + fileName + \".pdf\");\n\n                ConversionPreset intermediatePreset = new ConversionPreset(\"Pdf to image\", this.ConversionPreset, \"pdf\");\n                this.pdf2ImageConversionJob = ConversionJobFactory.Create(intermediatePreset, this.intermediateFilePath);\n                this.pdf2ImageConversionJob.PrepareConversion(this.OutputFilePaths);\n            }\n        }\n\n        protected override void Convert()\n        {\n            if (this.ConversionPreset == null)\n            {\n                throw new System.Exception(\"The conversion preset must be valid.\");\n            }\n\n            this.UserState = Properties.Resources.ConversionStateReadDocument;\n\n            if (!this.TryLoadDocumentIfNecessary())\n            {\n                this.ConversionFailed(Properties.Resources.ErrorUnableToUseMicrosoftOffice);\n                return;\n            }\n\n            // Make this document the active document.\n            this.document.Activate();\n\n            this.UserState = Properties.Resources.ConversionStateConversion;\n\n            Debug.Log(\"Convert word document to pdf.\");\n            // this.document.ExportAsFixedFormat(this.intermediateFilePath, Word.WdExportFormat.wdExportFormatPDF);\n            this.document.ExportAsFixedFormat(this.intermediateFilePath, ","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/Tichau/FileConverter/blob/6c157a411fb70fb7440d2e3b226042781fd55b15/Application/FileConverter/ConversionJobs/ConversionJob_Word.cs#L68-L104","documentation":"ConversionJob_Word.Convert() throws System.Exception when this.ConversionPreset is null before the NetOffice Word interop (activate document, export intermediate PDF, then run the pdf2Image sub-job). The base ctor rejects null presets, so this fires only via the parameterless ConversionJob_Word() design-mode constructor or direct/reflection invocation; Initialize() re-asserts the same invariant. A null preset would otherwise NRE when constructing the 'Pdf to image' intermediate ConversionPreset and the pdf2ImageConversionJob.","triggerScenarios":"A ConversionJob_Word created via its parameterless ctor has Convert() invoked; or reflection calls Convert() on an instance whose preset is null. The factory routes .docx/.doc/.odt inputs here and requires a non-null preset, so the normal path cannot reach the guard.","commonSituations":"XAML design-time instantiation; a unit test calling Convert() directly; a Word-conversion pipeline that created the job before the preset was deserialized from settings.","solutions":["Construct via new ConversionJob_Word(conversionPreset, inputFilePath) or ConversionJobFactory.Create.","Use PrepareConversion() + StartConversion(); do not call Convert() directly.","Early-return in design-time when ConversionPreset is null.","Confirm preset loading succeeded before job creation."],"exampleFix":"// before\nvar job = new ConversionJob_Word();\njob.Convert(); // throws: preset null\n\n// after\nvar job = new ConversionJob_Word(conversionPreset, inputFilePath);\njob.PrepareConversion(outputFilePaths);\njob.StartConversion();","handlingStrategy":"validation","validationCode":"if (job == null || job.ConversionPreset == null)\n{\n    throw new InvalidOperationException(\n        \"ConversionJob_Word requires a non-null ConversionPreset; use the (preset, inputFilePath) constructor.\");\n}\njob.StartConversion();","typeGuard":"private static bool HasValidPreset(ConversionJob job) => job != null && job.ConversionPreset != null;","tryCatchPattern":"try\n{\n    job.StartConversion();\n}\ncatch (Exception ex) when (ex.Message.Contains(\"conversion preset must be valid\"))\n{\n    logger.Error(ex, \"Word job created without a preset.\");\n    job = new ConversionJob_Word(LoadPreset(), inputPath);\n}","preventionTips":["Construct Word jobs via the (preset, path) ctor or ConversionJobFactory.Create.","Never call Convert() directly; use StartConversion().","Load and validate the preset from settings before creating the job.","Keep the parameterless ctor for XAML design time only."],"tags":["csharp","fileconverter","conversion-preset","null-check","defensive-guard","word","office-interop"],"backgroundTag":null,"analyzedSha":"6c157a411fb70fb7440d2e3b226042781fd55b15","analyzedAt":"2026-08-13T15:04:08.307Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}