{"record":{"id":"495b9e4682b619e3","repo":"HandyOrg/HandyControl","slug":"argumentnullexception-interopvalues","errorCode":null,"errorMessage":"ArgumentNullException","messagePattern":"ArgumentNullException","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Shared/HandyControl_Shared/Tools/Interop/InteropValues.cs","lineNumber":838,"sourceCode":"        public int Flags;\n        public int Version;\n        public int SigCount;\n        public int SigSize;\n\n        public IntPtr SigPattern = IntPtr.Zero;\n        public IntPtr SigMask = IntPtr.Zero;\n    }\n\n    internal class ComStreamFromDataStream : IStream\n    {\n        protected Stream DataStream;\n\n        // to support seeking ahead of the stream length...\n        private long _virtualPosition = -1;\n\n        internal ComStreamFromDataStream(Stream dataStream)\n        {\n            this.DataStream = dataStream ?? throw new ArgumentNullException(nameof(dataStream));\n        }\n\n        private void ActualizeVirtualPosition()\n        {\n            if (_virtualPosition == -1) return;\n\n            if (_virtualPosition > DataStream.Length)\n                DataStream.SetLength(_virtualPosition);\n\n            DataStream.Position = _virtualPosition;\n\n            _virtualPosition = -1;\n        }\n\n        public virtual IStream Clone()\n        {\n            NotImplemented();\n            return null;","sourceCodeStart":820,"sourceCodeEnd":856,"githubUrl":"https://github.com/HandyOrg/HandyControl/blob/2c0875ebd67326e0c67282967e3e809c69282fee/src/Shared/HandyControl_Shared/Tools/Interop/InteropValues.cs#L820-L856","documentation":"ComStreamFromDataStream wraps a managed System.IO.Stream so it can be exposed as a COM IStream. Its constructor requires a non-null stream and throws ArgumentNullException(name: \"dataStream\") when null is passed. This guards the internal COM interop adapter from later NullReferenceExceptions.","triggerScenarios":"Constructing ComStreamFromDataStream with a null Stream reference — typically when a factory returned null (e.g. File.Open failed upstream and null was passed on) and the result is fed into the COM stream adapter.","commonSituations":"Clipboard/OLE drag-drop interop code that streams data, resource loading where a stream lookup (GetManifestResourceStream) returned null and was forwarded unchecked.","solutions":["Ensure the Stream passed in is non-null: check the result of File.Open/GetManifestResourceStream/Assembly.GetStream before constructing.","If the stream may legitimately be missing, throw or report a file-not-found error at the source instead of forwarding null.","Catch ArgumentNullException at the interop boundary and map it to a meaningful 'data stream unavailable' error."],"exampleFix":"// before\nvar comStream = new ComStreamFromDataStream(GetResourceStream(name)); // null if missing\n\n// after\nvar stream = GetResourceStream(name);\nif (stream == null) throw new FileNotFoundException($\"Resource '{name}' not found.\");\nvar comStream = new ComStreamFromDataStream(stream);","handlingStrategy":"type-guard","validationCode":"if (stream == null) throw new FileNotFoundException(\"Data stream is required but was not found.\");","typeGuard":"static bool HasStream(Stream s) => s != null;","tryCatchPattern":"try { comStream = new ComStreamFromDataStream(stream); }\ncatch (ArgumentNullException) { comStream = null; ReportMissingStream(); }","preventionTips":["Never forward the result of stream-lookup APIs without a null check.","Fail fast at the source with FileNotFoundException instead of passing null onward.","Use pattern `stream ?? throw new FileNotFoundException(...)` at retrieval sites.","In tests, assert streams resolve before exercising COM interop paths."],"tags":["null-argument","stream","com-interop","wpf"],"backgroundTag":"null-argument","analyzedSha":"2c0875ebd67326e0c67282967e3e809c69282fee","analyzedAt":"2026-09-14T14:45:29.754Z","contentChangedAt":"2026-09-14T14:45:29.754Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}