{"record":{"id":"d192d471de1015d5","repo":"sschmid/Entitas","slug":"cannot-get-component-contextinfo-componentnames-index-from","errorCode":null,"errorMessage":"Cannot get component '{_contextInfo.ComponentNames[index]}' from {this}!","messagePattern":"Cannot get component '(.+?)' from (.+?)!","errorType":"exception","errorClass":"EntityDoesNotHaveComponentException","httpStatus":null,"severity":"error","filePath":"src/Entitas/Entity/Entity.cs","lineNumber":215,"sourceCode":"                    OnComponentRemoved?.Invoke(this, index, previousComponent);\n                }\n\n                GetComponentPool(index).Push(previousComponent);\n            }\n            else\n            {\n                OnComponentReplaced?.Invoke(this, index, previousComponent, newComponent);\n            }\n        }\n\n        /// Returns a component at the specified index.\n        /// You can only get a component at an index if it exists.\n        /// The preferred way is to use the\n        /// generated methods from the code generator.\n        public IComponent GetComponent(int index)\n        {\n            if (!HasComponent(index))\n                throw new EntityDoesNotHaveComponentException(index,\n                    $\"Cannot get component '{_contextInfo.ComponentNames[index]}' from {this}!\",\n                    \"You should check if an entity has the component before getting it.\");\n\n            return _components[index];\n        }\n\n        /// Returns all added components.\n        public IComponent[] GetComponents()\n        {\n            if (_componentsCache == null)\n            {\n                for (var i = 0; i < _components.Length; i++)\n                {\n                    var component = _components[i];\n                    if (component != null)\n                        _componentBuffer.Add(component);\n                }\n","sourceCodeStart":197,"sourceCodeEnd":233,"githubUrl":"https://github.com/sschmid/Entitas/blob/37547d1bd280f3f1c1ef7edb44f2a206660fbfb4/src/Entitas/Entity/Entity.cs#L197-L233","documentation":"Thrown by Entity.GetComponent when the component at the given index does not exist on the entity. Reading a component in Entitas requires that it is present; there is no null-return contract, so a missing component is an exception.","triggerScenarios":"Calling entity.GetComponent(index) or a generated e.position when entity.hasPosition is false; reading after RemoveComponent; reading in a system that matched a broader group than the component requires.","commonSituations":"Group matching on one component while reading another; ordering bugs where a removal system runs before the reader; typos confusing similar components.","solutions":["Use the generated has-check: if (entity.hasPosition) { var p = entity.position; }","Call entity.HasComponent(index) before GetComponent for raw-index access","Match the group on all components you intend to read (e.g. Matcher.AllOf(...))","Restructure so removal happens after all readers processed the frame"],"exampleFix":"// before\nvar pos = entity.position; // throws when !hasPosition\n// after\nif (entity.hasPosition) {\n    var pos = entity.position;\n    // use pos\n}","handlingStrategy":"type-guard","validationCode":"if (entity.HasComponent(index)) { var c = entity.GetComponent(index); }","typeGuard":"bool TryGetComponent(Entity e, int index, out IComponent c) { c = e.HasComponent(index) ? e.GetComponent(index) : null; return c != null; }","tryCatchPattern":"try { var c = entity.GetComponent(index); }\ncatch (EntityDoesNotHaveComponentException) { c = null; }","preventionTips":["Use generated properties behind the has-check (if (e.hasX) var x = e.x;)","Match groups with Matcher.AllOf for all components you read","Verify system execution order so removals never precede reads"],"tags":["csharp","entitas","component","precondition"],"backgroundTag":"entity-not-found","analyzedSha":"37547d1bd280f3f1c1ef7edb44f2a206660fbfb4","analyzedAt":"2026-09-14T00:26:23.246Z","contentChangedAt":"2026-09-14T00:26:23.246Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}