{"record":{"id":"0113a47a8ff515b9","repo":"PHPOffice/PHPWord","slug":"image-object-not-assigned","errorCode":null,"errorMessage":"Image object not assigned.","messagePattern":"Image object not assigned\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/PhpWord/Media.php","lineNumber":69,"sourceCode":"        $mediaId = md5($container . $source);\n        if (!isset(self::$elements[$container])) {\n            self::$elements[$container] = [];\n        }\n\n        // Add media if not exists or point to existing media\n        if (!isset(self::$elements[$container][$mediaId])) {\n            $mediaCount = self::countElements($container);\n            $mediaTypeCount = self::countElements($container, $mediaType);\n            ++$mediaTypeCount;\n            $rId = ++$mediaCount;\n            $target = null;\n            $mediaData = ['mediaIndex' => $mediaTypeCount];\n\n            switch ($mediaType) {\n                // Images\n                case 'image':\n                    if (null === $image) {\n                        throw new Exception('Image object not assigned.');\n                    }\n                    $isMemImage = $image->isMemImage();\n                    $extension = $image->getImageExtension();\n                    $mediaData['imageExtension'] = $extension;\n                    $mediaData['imageType'] = $image->getImageType();\n                    if ($isMemImage) {\n                        $mediaData['isMemImage'] = true;\n                        $mediaData['imageString'] = $image->getImageString();\n                    }\n                    $target = \"{$container}_image{$mediaTypeCount}.{$extension}\";\n                    $image->setTarget($target);\n                    $image->setMediaIndex($mediaTypeCount);\n\n                    break;\n                    // Objects\n                case 'object':\n                    $target = \"{$container}_oleObject{$mediaTypeCount}.bin\";\n","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/Media.php#L51-L87","documentation":"Media::addElement throws this when asked to register a media element of type 'image' but no Image instance was supplied. The library requires an actual Image object to derive extension, type, and content; without it the media relationship cannot be created.","triggerScenarios":"Calling addElement('image', ...) with a null $image parameter — e.g. Media::addElement('image', $mediaSection, $image) where $image was never constructed or an earlier lookup returned null.","commonSituations":"Conditional image loading that silently leaves the variable null; a helper returning null instead of an Image on failure; refactoring that drops the Image argument from the call.","solutions":["Construct a valid Image (e.g. via Image::fromFile or fromString) before calling Media::addElement","Guard the call: skip or throw a clearer error when $image is null","Check that any function returning the Image is not returning null on error paths"],"exampleFix":"// before\nMedia::addElement('image', $section, $image); // $image is null\n// after\nif ($image === null) {\n    $image = new Image($imagePath);\n}\nMedia::addElement('image', $section, $image);","handlingStrategy":"type-guard","validationCode":"if ($image === null) { throw new \\InvalidArgumentException('An Image object is required'); }","typeGuard":"function isImage($image): bool { return $image instanceof \\PhpOffice\\PhpWord\\Element\\Image; }","tryCatchPattern":"try { Media::addElement('image', $section, $image); } catch (\\Exception $e) { if ($e->getMessage() === 'Image object not assigned.') { /* handle missing image */ } throw $e; }","preventionTips":["Always construct the Image before media registration","Assert the Image instance with instanceof before calling","Avoid helper functions that may return null Image"],"tags":["phpword","null-argument","media"],"backgroundTag":"null-argument","analyzedSha":"aef95c04151b5633cc505f672ddd6a71da900ee1","analyzedAt":"2026-09-14T10:53:58.933Z","contentChangedAt":"2026-09-14T10:53:58.933Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}