{"record":{"id":"4318942a4640cb96","repo":"AtsushiSakai/PythonRobotics","slug":"state-machine-is-not-initialized","errorCode":null,"errorMessage":"State machine is not initialized","messagePattern":"State machine is not initialized","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"MissionPlanning/StateMachine/state_machine.py","lineNumber":236,"sourceCode":"        if isinstance(state, str):\n            self._state = self._get_state(state)\n        else:\n            self._state = state\n\n    def get_current_state(self):\n        return self._state\n\n    def process(self, event: str) -> None:\n        \"\"\"Process an event in the state machine.\n\n        Args:\n            event: Event name.\n\n        Example:\n            >>> machine.process(\"start\")\n        \"\"\"\n        if self._state is None:\n            raise ValueError(\"State machine is not initialized\")\n\n        if self._has_event(event):\n            self.state_transition(self._state, event)\n        else:\n            raise ValueError(f\"Invalid event: {event}\")\n\n    def generate_plantuml(self) -> str:\n        \"\"\"Generate PlantUML state diagram representation of the state machine.\n\n        Returns:\n            str: PlantUML state diagram code.\n        \"\"\"\n        if self._state is None:\n            raise ValueError(\"State machine is not initialized\")\n\n        plant_uml = [\"@startuml\"]\n        plant_uml.append(\"[*] --> \" + self._state.name)\n","sourceCodeStart":218,"sourceCodeEnd":254,"githubUrl":"https://github.com/AtsushiSakai/PythonRobotics/blob/1fe4fb980f6a12fe21c3c33d2b4da97a52c9154d/MissionPlanning/StateMachine/state_machine.py#L218-L254","documentation":"Raised by StateMachine.process when self._state is None, i.e. the machine was never given an initial state. Processing an event requires a current state to look up transitions.","triggerScenarios":"Creating a StateMachine, not setting the initial state (no initial_state/start call), then calling process('...') immediately.","commonSituations":"Forgetting the initialization step in setup code, or initialization failing silently earlier (e.g. exception during state setup) leaving _state unset.","solutions":["Set the initial state before processing events (call the machine's init/start API that assigns _state).","Verify initialization succeeded (assert machine._state is not None or a public accessor).","Move event subscriptions/loops to start only after machine setup completes."],"exampleFix":"# before\nmachine = StateMachine('m')\nmachine.process('start')\n\n# after\nmachine = StateMachine('m')\nmachine.set_initial_state(idle_state)  # or machine.initialize()\nmachine.process('start')","handlingStrategy":"validation","validationCode":"assert machine._state is not None, 'initialize the state machine before processing events'","typeGuard":"def is_initialized(machine) -> bool:\n    return machine._state is not None","tryCatchPattern":"try:\n    machine.process(event)\nexcept ValueError as e:\n    if 'not initialized' in str(e):\n        machine.initialize(); machine.process(event)\n    else:\n        raise","preventionTips":["Always run the machine's init/start routine right after construction.","Guard event loops with an is_initialized check."],"tags":["state-machine","initialization","lifecycle"],"backgroundTag":"uninitialized-component-use","analyzedSha":"1fe4fb980f6a12fe21c3c33d2b4da97a52c9154d","analyzedAt":"2026-08-28T13:23:33.733Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}