This is a migrated thread and some comments may be shown as answers.

Null reference exception from InsertFragment

8 Answers 152 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Mike Shilkov
Top achievements
Rank 1
Mike Shilkov asked on 03 May 2011, 08:42 AM
Hello.

I'm populating the document from strings to show it in RichTextBox. The code is very simple:
RadDocument document = new RadDocument();
Span span = new Span() { FontSize = 14, FontWeight = FontWeights.Bold };
span.Text = "123";
document.InsertInline(span);

But I'm getting the following exception:
System.NullReferenceException was caught
  StackTrace:
       в Telerik.Windows.Documents.DocumentStructure.DocumentStructureCollection.MergeContainers(LayoutBox fromBox)
       в Telerik.Windows.Documents.Model.RadDocument.InsertFragmentInternal(DocumentPosition position, DocumentFragment fragment)
       в Telerik.Windows.Documents.Model.RadDocument.InsertFragment(DocumentFragment fragment, DocumentPosition position)
       в Telerik.Windows.Documents.Model.RadDocument.InsertInline(Inline inline, DocumentPosition position)
       в Telerik.Windows.Documents.Model.RadDocument.InsertInline(Inline inline)
       в QESE.QFV.UI.Grid.Controls.HelpPopup.Populate(String title, HelpTexts texts)
  InnerException: 

Why?
---
Mike

8 Answers, 1 is accepted

Sort by
0
Mike Shilkov
Top achievements
Rank 1
answered on 04 May 2011, 09:15 AM
If I add some initialization code like

RadDocument document = new RadDocument();
Section section = new Section();
Paragraph paragraph = new Paragraph();
section.Blocks.Add(paragraph);
document.Sections.Add(section);
Span span = new Span() { FontSize = 14, FontWeight = FontWeights.Bold };
span.Text = "123";
document.InsertInline(span);

the exception callstack slightly changes
System.NullReferenceException was caught
  Message=В экземпляре объекта не задана ссылка на объект.
  StackTrace:
       в Telerik.Windows.Documents.Model.RadDocument.InsertInternal(String text, Span newSpanInstance, Span selectedSpanStyle, DocumentPosition positionToInsert)
       в Telerik.Windows.Documents.Model.RadDocument.InsertInternal(Span span, DocumentPosition positionToInsert)
       в Telerik.Windows.Documents.Model.RadDocument.InsertDocumentElementInternal(DocumentPosition position, DocumentElement elementToCopy)
       в Telerik.Windows.Documents.Model.RadDocument.InsertFragmentInternal(DocumentPosition position, DocumentFragment fragment)
       в Telerik.Windows.Documents.Model.RadDocument.InsertFragment(DocumentFragment fragment, DocumentPosition position)
       в Telerik.Windows.Documents.Model.RadDocument.InsertInline(Inline inline, DocumentPosition position)
       в Telerik.Windows.Documents.Model.RadDocument.InsertInline(Inline inline)
       в QESE.QFV.UI.Grid.Controls.HelpPopup.Populate(String title, HelpTexts texts)
  InnerException: 

0
Mike Shilkov
Top achievements
Rank 1
answered on 05 May 2011, 11:53 AM
Not important anymore... I was using 2010 version. Now upgraded to 2011, and solved my task with updated API.
0
Charles
Top achievements
Rank 1
answered on 14 Aug 2011, 03:36 AM
I have the 2011 version but I get the same exception. In debug mode this exception happens in the InsertInline method for my RadDocument, the document is not NULL.
0
Iva Toteva
Telerik team
answered on 17 Aug 2011, 03:50 PM
Hello Charles,

Since the issues Mike had encountered has been solved by upgrading to a later version, their cause is probably a different one.
Overall, when instantiating a RadDocument, a Section and Paragraphs manually, the Spans had to be added to the Inlines collection of the Paragraph instead of through the document's InsertInline method. This issue has been solved in the latest version of the controls.
In your scenario, InsertInline must work as expected. We'd be happy to address the particular problems you have met in the support ticket you opened.

Regards,
Iva
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Allen
Top achievements
Rank 2
Iron
Veteran
answered on 29 Sep 2011, 01:10 AM
Has this RadDocument.InsertInLines(span) thing been changed in the latest release? 
0
Iva Toteva
Telerik team
answered on 03 Oct 2011, 05:20 PM
Hi Allen,

We have not been able to reproduce the issue with InsertInline in the first place.
Could you give us a code-snippet, or a demo which reproduces the issue, so that we can identify the cause and make sure to fix the problem if it is still present with the latest version of the controls - the service pack of Q2.

Best wishes,
Iva
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Allen
Top achievements
Rank 2
Iron
Veteran
answered on 04 Oct 2011, 04:46 PM
You wrote:
Overall, when instantiating a RadDocument, a Section and Paragraphs manually, the Spans had to be added to the Inlines collection of the Paragraph instead of through the document's InsertInline method. This issue has been solved in the latest version of the controls.

So my question was:

Does this work Document.InsertLines(span)  now as stated above ?  Which release?

or does this till only work against Paragraph objects

0
Iva Toteva
Telerik team
answered on 07 Oct 2011, 04:15 PM
Hello Allen,

Let me elaborate a bit on the whole story behind the use of methods from the API and the programmatic building of RadDocuments.
The document has two states depending on if it has been measured or not. Some operations expect that the document has been measured, while others can only be performed on not measured documents. Manipulating the structure of the document explicitly (adding Inlines and Paragraphs to the Blocks and Inlines properties) can be performed on not measured documents only.
The document is measured by the Silverlight framework when shown in the editor. After that, you should use the methods from RadRichTextBox's API - InsertTable, InsertInline, InsertBookmark, etc. when you need to manipulate the content of the document. In previous versions of the control (before Q2 2011 Beta - 2011.2.623), methods from RadRichTextBox's and RadDocument's API could not be invoked on not-measure documents and explicit Measure and Arrange had to be invoked like this:

public void MeasureAndArrangeInDefaultSize(RadDocument document)
{
    document.Measure(RadDocument.MAX_DOCUMENT_SIZE);
    document.Arrange(new RectangleF(PointF.Empty, document.DesiredSize));
}

Starting from the Q2 2011 Beta, this is no longer needed, as a similar method is invoked internally when an operation that requires that the document be measured is invoked.
Therefore, the code from Mike's snippet works fine in the versions from 2011.2.623 on. The version of the controls that Charles was using was also an older one that 2011.2.623.
I hope this answers your question.

Regards,
Iva
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
RichTextBox
Asked by
Mike Shilkov
Top achievements
Rank 1
Answers by
Mike Shilkov
Top achievements
Rank 1
Charles
Top achievements
Rank 1
Iva Toteva
Telerik team
Allen
Top achievements
Rank 2
Iron
Veteran
Share this question
or