{"record":{"id":"405766d89bb3eb85","repo":"stride3d/stride","slug":"nameof-session-cannot-be-null-in-gettype-name","errorCode":null,"errorMessage":"[{nameof(Session)}] 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":27,"sourceCode":"public sealed class SessionTemplateGeneratorParameters : TemplateGeneratorParameters\n{\n    /// <summary>\n    /// Gets or sets the current session.\n    /// </summary>\n    /// <value>The session.</value>\n    public PackageSession Session { get; set; }\n\n    protected override void ValidateParameters()\n    {\n        base.ValidateParameters();\n\n        if (Description.Scope != TemplateScope.Session)\n        {\n            throw new InvalidOperationException($\"[{nameof(Description)}.{nameof(Description.Scope)}] must be {TemplateScope.Session} in {GetType().Name}\");\n        }\n        if (Session == null)\n        {\n            throw new InvalidOperationException($\"[{nameof(Session)}] cannot be null in {GetType().Name}\");\n        }\n    }\n}\n\npublic class PackageTemplateGeneratorParameters : TemplateGeneratorParameters\n{\n    public PackageTemplateGeneratorParameters()\n    {\n    }\n\n    public PackageTemplateGeneratorParameters(TemplateGeneratorParameters parameters, Package package)\n        : base(parameters)\n    {\n        Package = package;\n    }\n    /// <summary>\n    /// Gets or sets the package in which to execute this template\n    /// </summary>","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/assets/Stride.Core.Assets/Templates/TemplateGeneratorParameters.cs#L9-L45","documentation":"SessionTemplateGeneratorParameters.ValidateParameters throws this InvalidOperationException when the Session property (the PackageSession the template generator should operate on) has not been set. Stride requires a live session object for session-scoped template generation (e.g. creating a new project inside an open editor session), so it fails fast during Validate() instead of letting the generator crash later with a NullReferenceException.","triggerScenarios":"Calling TemplateGeneratorParameters.Validate() (which invokes ValidateParameters) on a SessionTemplateGeneratorParameters instance whose Session property is still null — typically after constructing the parameters object and setting Name/Description/Logger but forgetting to assign Session before generating a session-scope template.","commonSituations":"Custom tooling or editor plugins that create new projects via the template system but only partially populate the parameters; code copied from a Package/Asset template sample that doesn't set Session; running generation headlessly without loading a PackageSession first.","solutions":["Set the Session property to a valid, loaded PackageSession instance before calling Validate() or running the generator.","Load or create the PackageSession first (e.g. PackageSession.Load on the solution file) and pass it into the parameters.","If you do not have a session, use a template/generator parameters type whose scope does not require a session."],"exampleFix":"// before\nvar p = new SessionTemplateGeneratorParameters\n{\n    Name = \"MyGame\",\n    Description = templateDescription,\n    Logger = logger\n};\np.Validate(); // throws\n\n// after\nvar session = PackageSession.Load(solutionPath, logger);\nvar p = new SessionTemplateGeneratorParameters\n{\n    Name = \"MyGame\",\n    Description = templateDescription,\n    Logger = logger,\n    Session = session\n};\np.Validate();","handlingStrategy":"validation","validationCode":"if (p is SessionTemplateGeneratorParameters sp && sp.Session == null)\n    throw new InvalidOperationException(\"SessionTemplateGeneratorParameters.Session must be set to a loaded PackageSession before Validate().\");","typeGuard":"bool HasSession(SessionTemplateGeneratorParameters p) => p.Session is not null;","tryCatchPattern":"try { p.Validate(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(nameof(SessionTemplateGeneratorParameters.Session)))\n{\n    // load a PackageSession and retry or report\n}","preventionTips":["Always load or create the PackageSession before building session-scope parameters.","Set all required properties (Name, Description, Logger, Session) in one object-initializer block so none is skipped.","Add an assert/contract check for Session in your tooling's generation entry point."],"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"}