{"record":{"id":"2e84cb89468a0fd7","repo":"stride3d/stride","slug":"nameof-package-cannot-be-null-in-gettype-name","errorCode":null,"errorMessage":"[{nameof(Package)}] cannot be null in {GetType().Name}","messagePattern":"\\[(.+?)\\] cannot be null in (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/assets/Stride.Core.Assets/Templates/TemplateGeneratorParameters.cs","lineNumber":59,"sourceCode":"        Package = package;\n    }\n    /// <summary>\n    /// Gets or sets the package in which to execute this template\n    /// </summary>\n    /// <value>The package.</value>\n    public Package Package { get; set; }\n\n    protected override void ValidateParameters()\n    {\n        base.ValidateParameters();\n\n        if (Description.Scope != TemplateScope.Package && GetType() == typeof(PackageTemplateGeneratorParameters))\n        {\n            throw new InvalidOperationException($\"[{nameof(Description)}.{nameof(Description.Scope)}] must be {TemplateScope.Package} in {GetType().Name}\");\n        }\n        if (Package == null)\n        {\n            throw new InvalidOperationException($\"[{nameof(Package)}] cannot be null in {GetType().Name}\");\n        }\n    }\n}\n\npublic class AssetTemplateGeneratorParameters : PackageTemplateGeneratorParameters\n{\n    public AssetTemplateGeneratorParameters(UDirectory targetLocation, IEnumerable<UFile>? sourceFiles = null)\n    {\n        TargetLocation = targetLocation;\n        SourceFiles = [];\n        if (sourceFiles != null)\n            SourceFiles.AddRange(sourceFiles);\n    }\n\n    public UDirectory TargetLocation { get; }\n\n    public List<UFile> SourceFiles { get; }\n","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/assets/Stride.Core.Assets/Templates/TemplateGeneratorParameters.cs#L41-L77","documentation":"PackageTemplateGeneratorParameters.ValidateParameters throws this InvalidOperationException when the Package property — the Stride Package in which the template generator executes — is null. A package-scoped template needs a target package to modify or add projects to, so validation fails fast during Validate() instead of crashing inside the generator with a NullReferenceException.","triggerScenarios":"Calling Validate() on a PackageTemplateGeneratorParameters (or AssetTemplateGeneratorParameters, which inherits the check) whose Package property was never assigned — e.g. constructed via the default constructor and only Name/Description/Logger were set.","commonSituations":"Tooling that generates assets or packages into a currently open Stride project but never resolves the Package object from the session; passing a null package through the PackageTemplateGeneratorParameters(parameters, package) constructor; scripts running outside the editor without loading the package.","solutions":["Assign a valid Package instance to the Package property before calling Validate().","Resolve the target package from the loaded session, e.g. session.Packages.Find(...), or load it with Package.Load.","If you meant to generate without a package, use SessionTemplateGeneratorParameters (session scope) instead."],"exampleFix":"// before\nvar p = new AssetTemplateGeneratorParameters(targetLocation)\n{\n    Name = \"MySprite\",\n    Description = desc,\n    Logger = logger\n    // Package never set\n};\np.Validate(); // throws\n\n// after\np.Package = session.CurrentPackage; // or Package.Load(packagePath, logger)\np.Validate();","handlingStrategy":"validation","validationCode":"if (p is PackageTemplateGeneratorParameters pp && pp.Package == null)\n    throw new InvalidOperationException(\"Package must be resolved from the session (or loaded) before Validate().\");","typeGuard":"bool HasPackage(PackageTemplateGeneratorParameters p) => p.Package is not null;","tryCatchPattern":"try { p.Validate(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(nameof(PackageTemplateGeneratorParameters.Package)))\n{\n    // resolve the target Package and retry\n}","preventionTips":["Resolve the target package from the open PackageSession before constructing parameters.","Never pass null for the package parameter of the PackageTemplateGeneratorParameters(parameters, package) constructor.","Validate parameters in a single helper that checks Session/Package/Description together."],"tags":["csharp","null-argument","templates","validation"],"backgroundTag":"null-argument","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}