stride3d/stride · error · InvalidOperationException
[ ] cannot be null in
Error message
[{nameof(Package)}] cannot be null in {GetType().Name} What it means
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.
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.
Example fix
// before
var p = new AssetTemplateGeneratorParameters(targetLocation)
{
Name = "MySprite",
Description = desc,
Logger = logger
// Package never set
};
p.Validate(); // throws
// after
p.Package = session.CurrentPackage; // or Package.Load(packagePath, logger)
p.Validate(); Defensive patterns
Strategy: validation
Validate before calling
if (p is PackageTemplateGeneratorParameters pp && pp.Package == null)
throw new InvalidOperationException("Package must be resolved from the session (or loaded) before Validate()."); Type guard
bool HasPackage(PackageTemplateGeneratorParameters p) => p.Package is not null;
Try / catch
try { p.Validate(); }
catch (InvalidOperationException ex) when (ex.Message.Contains(nameof(PackageTemplateGeneratorParameters.Package)))
{
// resolve the target Package and retry
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- [ ] cannot be null in
- [ ] cannot be null in
- [ ] cannot be null in
- [ ] cannot be null in
- [ ] cannot be null in
AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14).
Data as JSON: /api/errors/2e84cb89468a0fd7.
Report an issue: GitHub.
Appendix: source
Thrown at sources/assets/Stride.Core.Assets/Templates/TemplateGeneratorParameters.cs:59
Package = package;
}
/// <summary>
/// Gets or sets the package in which to execute this template
/// </summary>
/// <value>The package.</value>
public Package Package { get; set; }
protected override void ValidateParameters()
{
base.ValidateParameters();
if (Description.Scope != TemplateScope.Package && GetType() == typeof(PackageTemplateGeneratorParameters))
{
throw new InvalidOperationException($"[{nameof(Description)}.{nameof(Description.Scope)}] must be {TemplateScope.Package} in {GetType().Name}");
}
if (Package == null)
{
throw new InvalidOperationException($"[{nameof(Package)}] cannot be null in {GetType().Name}");
}
}
}
public class AssetTemplateGeneratorParameters : PackageTemplateGeneratorParameters
{
public AssetTemplateGeneratorParameters(UDirectory targetLocation, IEnumerable<UFile>? sourceFiles = null)
{
TargetLocation = targetLocation;
SourceFiles = [];
if (sourceFiles != null)
SourceFiles.AddRange(sourceFiles);
}
public UDirectory TargetLocation { get; }
public List<UFile> SourceFiles { get; }
View on GitHub (pinned to 96fad776d2)