An unhandled exception has occurred while executing the request. System.ArgumentNullException: Value cannot be null. (Parameter 'buffer')

Question: Why is MemoryStream having a Null buffer even after checking if the Stream contains any bytes?
The error says "An unhandled exception has occurred while executing the request.
      System.ArgumentNullException: Value cannot be null. (Parameter 'buffer')"


Answer: The reason for the error "An unhandled exception has occurred while executing the request.System.ArgumentNullException: Value cannot be null. (Parameter 'buffer')" is due to MemoryStream being Null, make sure that you are using the correct directive to check for Null buffer in the Memory Stream.

Use the keyword "is null" in C# like below:

if(memeryStream is not null && memoryStream.Lenght > 0)
//Execute

The null checking will execute first before checking the length, if you check the length first the Exception will throw.


Write an Article
If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.


© 2022 - ErnesTech - Privacy