{"record":{"id":"ad533c32dde8b8c9","repo":"antlr/antlr4","slug":"target-cannot-be-null-ad533c","errorCode":null,"errorMessage":"target cannot be null.","messagePattern":"target cannot be null\\.","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"runtime/CSharp/src/Atn/Transition.cs","lineNumber":37,"sourceCode":"    /// we can fix these transitions as specific classes. The DFA transitions\n    /// on the other hand need to update the labels as it adds transitions to\n    /// the states. We'll use the term Edge for the DFA to distinguish them from\n    /// ATN transitions.</p>\n    /// </remarks>\n    public abstract class Transition\n    {\n        public static readonly ReadOnlyCollection<string> serializationNames = new ReadOnlyCollection<string>(Arrays.AsList(\"INVALID\", \"EPSILON\", \"RANGE\", \"RULE\", \"PREDICATE\", \"ATOM\", \"ACTION\", \"SET\", \"NOT_SET\", \"WILDCARD\", \"PRECEDENCE\"));\n\n        /// <summary>The target of this transition.</summary>\n        /// <remarks>The target of this transition.</remarks>\n        [NotNull]\n        public ATNState target;\n\n        protected internal Transition(ATNState target)\n        {\n            if (target == null)\n            {\n                throw new ArgumentNullException(\"target cannot be null.\");\n            }\n            this.target = target;\n        }\n\n        public abstract TransitionType TransitionType\n        {\n            get;\n        }\n\n        /// <summary>Determines if the transition is an \"epsilon\" transition.</summary>\n        /// <remarks>\n        /// Determines if the transition is an \"epsilon\" transition.\n        /// <p>The default implementation returns\n        /// <see langword=\"false\"/>\n        /// .</p>\n        /// </remarks>\n        /// <returns>\n        ///","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/CSharp/src/Atn/Transition.cs#L19-L55","documentation":"The abstract Transition constructor requires a non-null target state; every edge in an ATN must point somewhere. The runtime checks this at construction time and throws ArgumentNullException otherwise. This is internal/runtime-level API: user code almost never constructs transitions directly, so seeing this error usually means code is hand-building or mutating ATN structures.","triggerScenarios":"Calling any Transition subclass constructor (AtomTransition, RangeTransition, SetTransition, RuleTransition, etc.) with target == null. Happens in custom ATN construction, test code that builds ATNs by hand, or code that copies/rewires transitions and passes a null target.","commonSituations":"Writing a custom serializer/deserializer or ATN optimizer; cloning ATNs; test harnesses synthesizing small ATNs and forgetting to create the target state first.","solutions":["Create the target ATNState before creating the transition that points to it","If building transitions from data, validate targetState != null before the constructor call and report which edge index was malformed","Prefer mutating an existing deserialized ATN over constructing one from scratch"],"exampleFix":"// before\nvar t = new AtomTransition(null, label);\n\n// after\nvar target = new BasicState { ruleIndex = 0 };\natn.AddState(target);\nvar t = new AtomTransition(target, label);","handlingStrategy":"validation","validationCode":"ATNState target = GetTargetState() ?? throw new InvalidOperationException($\"Edge {edgeIndex} has no target state\");\natn.AddState(target);\nvar t = new AtomTransition(target, label);","typeGuard":"static bool IsValidTransitionTarget(ATNState s) => s != null && s.stateNumber >= 0;","tryCatchPattern":null,"preventionTips":["When hand-building ATNs, create and register all states before any transitions","Validate edge data (from,to both present) before constructing transitions in custom deserializers"],"tags":["antlr","csharp","atn","transition","null-check"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}