Getting exception on loading docx file

Getting System.NullReferenceException: 'Object reference not set to an instance of an object.'  on 

                DocumentEditorModule editor = container.GetDocumentEditor();
                await editor.Open(sfdtString);

I'm  writing code on ComponentBase class, not in razor file itself

Razor file
inherits DocumentViewComponentBase;
<SfDocumentEditorContainer @ref="container" EnableToolbar=true RestrictEditing=true>
</SfDocumentEditorContainer>


DocumentViewComponentBase
        protected string DocxString;
        protected SfDocumentEditorContainer docContainer;

        public DocumentViewComponentBase()
        {
            docContainer = new SfDocumentEditorContainer(); // Before initializing docContainer getting excetion at DocumentEditorModule editor = container.GetDocumentEditor(); but after initializing it in constructor getting exception at await editor.Open(sfdtString);
        }

        private async Task GetDocumentDetails(string filename)
        {
                    // Get a reference to a blob
                    BlobClient blobClient = await Task.FromResult(containerClient.GetBlobClient(filename));

                    if (blobClient is null)
                        return;

                    // Download the blob's contents and save it to a file
                    BlobDownloadInfo downloadInfo = await blobClient.DownloadAsync();

                    using MemoryStream memoryStream = new MemoryStream();
                    await downloadInfo.Content.CopyToAsync(memoryStream);
                    memoryStream.Position = 0;

                    if (downloadInfo?.ContentType == "application/msword")
                    {
                        DocumentType = "doc";

                        WordDocument document = WordDocument.Load(memoryStream, ImportFormatType.Docx);
                        DocxString = JsonConvert.SerializeObject(document);
                        document.Dispose();

                        DocumentEditorModule editor = docContainer.GetDocumentEditor();
                        await editor.Open(DocxString); // Getting System.NullReferenceException: 'Object reference not set to an instance of an object.' here
                    }

                 memoryStream.Dispose();
                StateHasChanged();
        }

3 Replies 1 reply marked as answer

SR Stephen Raj Chandra Sekar Syncfusion Team November 26, 2020 04:58 PM UTC

Hi Richy, 
  
Thanks for contacting Syncfusion support. 
 
You are using ‘SfDocumentEditorContainer’ instance before control getting initialized, So "null reference exception" occurs. 
 
We have an event ‘Created’ in ‘SfDocumentEditorContainer’ which triggers after the control initialization. So, kindly open the file in “Created” event. Please find the modified code snippet below. 
 
Razor file 
@using Syncfusion.Blazor.DocumentEditor 
@inherits DocumentViewComponentBase; 
  
<SfDocumentEditorContainer @ref="container" Height="700px" EnableToolbar=true RestrictEditing=true> 
    <DocumentEditorContainerEvents Created="OnCreated"></DocumentEditorContainerEvents> 
</SfDocumentEditorContainer> 
  
@code { 
    protected void OnCreated(object args) 
    { 
        base.OpenDocument(); 
    } 
} 
  
DocumentViewComponentBase 
 public class DocumentViewComponentBase : ComponentBase 
    { 
        protected string DocxString; 
        protected SfDocumentEditorContainer container; 
        public DocumentViewComponentBase() 
        { 
        } 
        public void OpenDocument() 
        { 
            // Get Document Editor instance now. 
            DocumentEditorModule editor = container.GetDocumentEditor(); 
            // Handle file opening here. 
            //editor.Open(sfdt); 
        } 
        private async Task GetDocumentDetails(string filename) 
        { 
            // Get a reference to a blob 
        } 
    } 
  
Kindly download the modified sample from below link and let us know whether it helps you. 
 
   
Regards, 
Stephen Raj 


Marked as answer

RI Richy November 27, 2020 10:15 AM UTC

Thanks it works


HC Harini Chellappa Syncfusion Team November 30, 2020 08:07 AM UTC

Hi Richy, 
 
Welcome! 
 
Regards, 
 
Harini C 


Loader.
Up arrow icon