Skip to content
Back

NullReferenceException whenever I call storage.CreateFile(...) in the .NET C# SDK.

  • 0
  • Storage
🔥FlamingMan🔥
19 Jun, 2025, 19:00

I hope you’re doing well. I’m encountering a NullReferenceException: Object reference not set to an instance of an object. whenever I call storage.CreateFile(...) in the .NET C# SDK. I’ve spent a lot of time debugging and I can’t pinpoint whether this is a mistake in my code or an SDK issue.

Below is a minimal summary of my environment and reproduction steps:

TypeScript
// 1) I fetch the existing user document to read the old avatar ID
var doc = await databases.GetDocument(dbId, logopedistCollectionId, user.Id);
var oldAvatarId = doc.Data["avatarId"] as string;

// 2) I decide I need to replace the image
//    – so I delete the old file if it exists
if (!string.IsNullOrEmpty(oldAvatarId))
    await storage.DeleteFile(bucketId, oldAvatarId);

// 3) I prepare the new image
var imageBytes = Convert.FromBase64String(updateModel.Photo.Data);
var inputFile  = InputFile.FromBytes(imageBytes, updateModel.Photo.Name, mimeType);

// 4) **This line throws a NullReferenceException internally in the SDK**:  
await storage.CreateFile(bucketId, newFileId, inputFile);

🔍 What I’ve Checked Bucket ID ‑ Verified that bucketId is the correct Storage bucket (not the database ID).

InputFile ‑ Confirmed imageBytes is non‑null/empty. ‑ inputFile.Name and inputFile.MimeType are both non‑null, non‑empty strings.

Permissions ‑ Confirmed my service API key has full read/write access to the bucket.

Alternative Workarounds ‑ Tried uploading via JavaScript SDK to the same bucket with an identical file → works fine. ‑ Tested uploading a small “hello world” text file via C# → succeeds.

So the only failing scenario is uploading a binary image via InputFile.FromBytes(…, “avatar.png”, “image/png”).

TL;DR
Issue: NullReferenceException occurs when calling storage.CreateFile(...) in the .NET C# SDK. Summary: Developer experiencing NullReferenceException when using storage.CreateFile in the .NET C# SDK. Code logic confirmed, input data validated, and permissions checked. Issue isolated to uploading binary images via InputFile.FromBytes. Solution: Consider reviewing the implementation of InputFile.FromBytes(), ensure correct instantiation of objects to avoid NullReferenceException when invoking storage.CreateFile(...).
Kenny
19 Jun, 2025, 19:20

Do you have the full stack trace you can provide?

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more