{"record":{"id":"ca7378a3c31fbdd8","repo":"MonoGame/MonoGame","slug":"no-data-set-on-bitmap","errorCode":null,"errorMessage":"No data set on bitmap","messagePattern":"No data set on bitmap","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"MonoGame.Framework.Content.Pipeline/Graphics/PvrtcBitmapContent.cs","lineNumber":32,"sourceCode":"    {\n        private byte[] _bitmapData = [];\n\n        private int GetDataSize()\n        {\n            TryGetFormat(out var format);\n            return format switch\n            {\n                SurfaceFormat.RgbPvrtc2Bpp or SurfaceFormat.RgbaPvrtc2Bpp => (Math.Max(Width, 16) * Math.Max(Height, 8) * 2 + 7) / 8,\n                SurfaceFormat.RgbPvrtc4Bpp or SurfaceFormat.RgbaPvrtc4Bpp => (Math.Max(Width, 8) * Math.Max(Height, 8) * 4 + 7) / 8,\n                _ => 0,\n            };\n        }\n\n        /// <inheritdoc/>\n        public override byte[] GetPixelData()\n        {\n            if (_bitmapData == null)\n                throw new InvalidOperationException(\"No data set on bitmap\");\n            var result = new byte[_bitmapData.Length];\n            Buffer.BlockCopy(_bitmapData, 0, result, 0, _bitmapData.Length);\n            return result;\n        }\n\n        /// <inheritdoc/>\n        public override void SetPixelData(byte[] sourceData)\n        {\n            var size = GetDataSize();\n            if (sourceData.Length != size)\n                throw new ArgumentException(\"Incorrect data size. Expected \" + size + \" bytes\");\n            if (_bitmapData.Length != size)\n                _bitmapData = new byte[size];\n            Buffer.BlockCopy(sourceData, 0, _bitmapData, 0, size);\n        }\n\n        /// <inheritdoc/>\n        protected override bool TryCopyFrom(BitmapContent sourceBitmap, Rectangle sourceRegion, Rectangle destinationRegion)","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/MonoGame/MonoGame/blob/1d71bbd0ff4071a03caa583aa3bc56b85924a819/MonoGame.Framework.Content.Pipeline/Graphics/PvrtcBitmapContent.cs#L14-L50","documentation":"Thrown by PvrtcBitmapContent.GetPixelData() when _bitmapData is null. NOTE: the field is initialized to an empty array (_bitmapData = []), so this null check is effectively dead code under normal usage — the field is never null after construction. The intended guard is against reading pixel data before SetPixelData has been called. In practice this exception is very difficult to trigger through public API because the initializer prevents null.","triggerScenarios":"Calling GetPixelData() before SetPixelData() on a PvrtcBitmapContent — though the actual null check rarely fires because _bitmapData defaults to an empty array rather than null. Could theoretically occur if the field is set to null via reflection or serialization deserialization.","commonSituations":"Custom PVRTC compression processors that read before writing; deserialization edge cases that leave the field null; reflection-based code that interferes with field initialization.","solutions":["Always call SetPixelData() before GetPixelData() on PvrtcBitmapContent instances","This specific null-check path is likely unreachable given the field initializer _bitmapData = []; if you hit it, check for reflection or serialization tampering","File a bug report — the check should probably test _bitmapData.Length == 0 rather than == null"],"exampleFix":"// before\nvar data = pvrBitmap.GetPixelData(); // may throw if no data set\n\n// after\npvrBitmap.SetPixelData(sourceBytes);\nvar data = pvrBitmap.GetPixelData();","handlingStrategy":"validation","validationCode":"// Ensure SetPixelData was called before GetPixelData.\n// Note: _bitmapData is initialized to [] not null, so the null check\n// in the library is effectively dead code. Check for empty data instead\n// by catching the empty-result scenario at the application level.\nvar data = pvrBitmap.GetPixelData();\nif (data.Length == 0)\n    /* no pixel data has been set yet */","typeGuard":null,"tryCatchPattern":"try { var data = pvrBitmap.GetPixelData(); }\ncatch (InvalidOperationException ex) when (ex.Message == \"No data set on bitmap\")\n{ /* call SetPixelData first */ }","preventionTips":["Always call SetPixelData before GetPixelData on PvrtcBitmapContent","Note: the library's null check is unreachable since _bitmapData defaults to []; the practical issue is empty data","If you encounter this exception, suspect reflection or deserialization resetting the field to null"],"tags":["texture","bitmap","pvrtc","content-pipeline","monogame"],"backgroundTag":null,"analyzedSha":"1d71bbd0ff4071a03caa583aa3bc56b85924a819","analyzedAt":"2026-08-13T17:10:33.625Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}