HandyOrg/HandyControl · error · ArgumentNullException
ArgumentNullException
Error message
ArgumentNullException
What it means
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.
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.
Example fix
// before
var comStream = new ComStreamFromDataStream(GetResourceStream(name)); // null if missing
// after
var stream = GetResourceStream(name);
if (stream == null) throw new FileNotFoundException($"Resource '{name}' not found.");
var comStream = new ComStreamFromDataStream(stream); Defensive patterns
Strategy: type-guard
Validate before calling
if (stream == null) throw new FileNotFoundException("Data stream is required but was not found."); Type guard
static bool HasStream(Stream s) => s != null;
Try / catch
try { comStream = new ComStreamFromDataStream(stream); }
catch (ArgumentNullException) { comStream = null; ReportMissingStream(); } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Clipboard/OLE drag-drop interop code that streams data, resource loading where a stream lookup (GetManifestResourceStream) returned null and was forwarded unchecked.
Related errors
- Wrote an incorrect number of bytes
- Wrote an incorrect number of bytes
- stream null
- NotImplemented
- NotSupportedException
AI-assisted analysis of HandyOrg/HandyControl@2c0875ebd6 (2026-09-14).
Data as JSON: /api/errors/495b9e4682b619e3.
Report an issue: GitHub.
Appendix: source
Thrown at src/Shared/HandyControl_Shared/Tools/Interop/InteropValues.cs:838
public int Flags;
public int Version;
public int SigCount;
public int SigSize;
public IntPtr SigPattern = IntPtr.Zero;
public IntPtr SigMask = IntPtr.Zero;
}
internal class ComStreamFromDataStream : IStream
{
protected Stream DataStream;
// to support seeking ahead of the stream length...
private long _virtualPosition = -1;
internal ComStreamFromDataStream(Stream dataStream)
{
this.DataStream = dataStream ?? throw new ArgumentNullException(nameof(dataStream));
}
private void ActualizeVirtualPosition()
{
if (_virtualPosition == -1) return;
if (_virtualPosition > DataStream.Length)
DataStream.SetLength(_virtualPosition);
DataStream.Position = _virtualPosition;
_virtualPosition = -1;
}
public virtual IStream Clone()
{
NotImplemented();
return null;View on GitHub (pinned to 2c0875ebd6)