I am doing a search/replace in code using the RadDocument and I am getting a strange null error.
Here is the code:
When the RadDocument is simple,this works fine, however, when I put the Merge fields in Tables, it is throwing an error on the search.FindAll() method:
System.NullReferenceException: Object reference not set to an instance of an object.
" at Telerik.Windows.Documents.TextSearch.DocumentTextMap.MoveToFirstBoxInWord(DocumentPosition position) in c:\\TB\\102\\WPF_Scrum\\Release_WPF\\Sources\\Development\\Documents\\Core\\TextSearch\\DocumentTextMap.cs:line 313\r\n at Telerik.Windows.Documents.TextSearch.DocumentTextMap.InitMap(DocumentPosition fromPosition, DocumentPosition toPosition) in c:\\TB\\102\\WPF_Scrum\\Release_WPF\\Sources\\Development\\Documents\\Core\\TextSearch\\DocumentTextMap.cs:line 115\r\n at Telerik.Windows.Documents.TextSearch.DocumentTextSearch.FindInternal(String regEx, DocumentPosition fromPosition, DocumentPosition toPosition, Boolean findFirst) in c:\\TB\\102\\WPF_Scrum\\Release_WPF\\Sources\\Development\\Documents\\Core\\TextSearch\\DocumentTextSearch.cs:line 90\r\n at Telerik.Windows.Documents.TextSearch.DocumentTextSearch.FindAll(String regEx) in c:\\TB\\102\\WPF_Scrum\\Release_WPF\\Sources\\Development\\Documents\\Core\\TextSearch\\DocumentTextSearch.cs:line 52\r\n at Service.ServiceHandler.Documentation.DocDataMgr.MergeText(RadDocument doc, String mergeName, String mergeText) in N:\\BMS\\svn_r3\\Rf3Db\\Service\\ServiceHandler\\Documentation\\DocDataMgr.cs:line 947"
Here is the text I am searching for: "{ MERGEFIELD TmHeader }"
Here is the RadDocument:
Here is the code:
doc.Selection.Clear();
DocumentTextSearch search =
new
DocumentTextSearch(doc);
var rangesTrackingDocumentChanges =
new
List<TextRange>();
var resultRanges = search.FindAll(searchText);
foreach
(var textRange
in
resultRanges)
{
TextRange newRange =
new
TextRange(
new
DocumentPosition(textRange.StartPosition,
true
),
new
DocumentPosition(textRange.EndPosition,
true
));
rangesTrackingDocumentChanges.Add(newRange);
}
foreach
(var textRange
in
rangesTrackingDocumentChanges)
{
doc.CaretPosition.MoveToPosition(textRange.StartPosition);
doc.DeleteRange(textRange.StartPosition, textRange.EndPosition);
doc.Insert(mergeText,
new
Telerik.Windows.Documents.Model.Styles.StyleDefinition());
textRange.StartPosition.Dispose();
textRange.EndPosition.Dispose();
}
When the RadDocument is simple,this works fine, however, when I put the Merge fields in Tables, it is throwing an error on the search.FindAll() method:
System.NullReferenceException: Object reference not set to an instance of an object.
" at Telerik.Windows.Documents.TextSearch.DocumentTextMap.MoveToFirstBoxInWord(DocumentPosition position) in c:\\TB\\102\\WPF_Scrum\\Release_WPF\\Sources\\Development\\Documents\\Core\\TextSearch\\DocumentTextMap.cs:line 313\r\n at Telerik.Windows.Documents.TextSearch.DocumentTextMap.InitMap(DocumentPosition fromPosition, DocumentPosition toPosition) in c:\\TB\\102\\WPF_Scrum\\Release_WPF\\Sources\\Development\\Documents\\Core\\TextSearch\\DocumentTextMap.cs:line 115\r\n at Telerik.Windows.Documents.TextSearch.DocumentTextSearch.FindInternal(String regEx, DocumentPosition fromPosition, DocumentPosition toPosition, Boolean findFirst) in c:\\TB\\102\\WPF_Scrum\\Release_WPF\\Sources\\Development\\Documents\\Core\\TextSearch\\DocumentTextSearch.cs:line 90\r\n at Telerik.Windows.Documents.TextSearch.DocumentTextSearch.FindAll(String regEx) in c:\\TB\\102\\WPF_Scrum\\Release_WPF\\Sources\\Development\\Documents\\Core\\TextSearch\\DocumentTextSearch.cs:line 52\r\n at Service.ServiceHandler.Documentation.DocDataMgr.MergeText(RadDocument doc, String mergeName, String mergeText) in N:\\BMS\\svn_r3\\Rf3Db\\Service\\ServiceHandler\\Documentation\\DocDataMgr.cs:line 947"
Here is the text I am searching for: "{ MERGEFIELD TmHeader }"
Here is the RadDocument:
"<
t:RadDocument
xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:t=\"clr-namespace:Telerik.Windows.Documents.Model;
assembly
=
Telerik
.Windows.Documents\" xmlns:s=\"clr-namespace:Telerik.Windows.Documents.Model.Styles;
assembly
=
Telerik
.Windows.Documents\" version=\"1.2\" LayoutMode=\"Flow\" LineSpacing=\"1.14999997615814\" LineSpacingType=\"Auto\" ParagraphDefaultSpacingAfter=\"12\" ParagraphDefaultSpacingBefore=\"0\" SectionDefaultPageSize=\"816,1056\" StyleName=\"defaultDocumentStyle\">\r\n <
t:RadDocument.ProtectionSettings
>\r\n <
t:DocumentProtectionSettings
EnableDocumentProtection=\"False\" Enforce=\"False\" HashingAlgorithm=\"None\" HashingSpinCount=\"0\" ProtectionMode=\"ReadOnly\" />\r\n </
t:RadDocument.ProtectionSettings
>\r\n <
t:RadDocument.Styles
>\r\n <
s:StyleDefinition
DisplayName=\"Document Default Style\" IsCustom=\"False\" IsDefault=\"False\" IsPrimary=\"True\" Name=\"defaultDocumentStyle\" Type=\"Default\">\r\n <
s:StyleDefinition.ParagraphStyle
>\r\n <
s:ParagraphProperties
LineSpacing=\"1.14999997615814\" SpacingAfter=\"12\" />\r\n </
s:StyleDefinition.ParagraphStyle
>\r\n <
s:StyleDefinition.SpanStyle
>\r\n <
s:SpanProperties
FontFamily=\"Verdana\" FontSize=\"16\" FontStyle=\"Normal\" FontWeight=\"Normal\" />\r\n </
s:StyleDefinition.SpanStyle
>\r\n </
s:StyleDefinition
>\r\n <
s:StyleDefinition
BasedOnName=\"TableNormal\" DisplayName=\"TableGrid\" IsCustom=\"False\" IsDefault=\"False\" IsPrimary=\"True\" Name=\"TableGrid\" Type=\"Table\">\r\n <
s:StyleDefinition.ParagraphStyle
>\r\n <
s:ParagraphProperties
LineSpacing=\"1\" SpacingAfter=\"0\" />\r\n </
s:StyleDefinition.ParagraphStyle
>\r\n <
s:StyleDefinition.TableStyle
>\r\n <
s:TableProperties
Borders=\"1,Single,#FF000000\" />\r\n </
s:StyleDefinition.TableStyle
>\r\n </
s:StyleDefinition
>\r\n <
s:StyleDefinition
DisplayName=\"TableNormal\" IsCustom=\"False\" IsDefault=\"True\" IsPrimary=\"True\" Name=\"TableNormal\" Type=\"Table\" />\r\n </
t:RadDocument.Styles
>\r\n <
t:Section
>\r\n <
t:Paragraph
TextAlignment=\"Left\">\r\n <
t:Paragraph.TabStops
>\r\n <
t:TabStop
Position=\"208\" TabStopType=\"Left\" />\r\n </
t:Paragraph.TabStops
>\r\n </
t:Paragraph
>\r\n <
t:Paragraph
TextAlignment=\"Left\">\r\n <
t:Paragraph.TabStops
>\r\n <
t:TabStop
Position=\"208\" TabStopType=\"Left\" />\r\n </
t:Paragraph.TabStops
>\r\n <
t:Span
Text=\"{}{ MERGEFIELD TmDate } { MERGEFIELD TmLeft }{ MERGEFIELD TmRight }\" />\r\n </
t:Paragraph
>\r\n <
t:Paragraph
TextAlignment=\"Left\">\r\n <
t:Paragraph.TabStops
>\r\n <
t:TabStop
Position=\"208\" TabStopType=\"Left\" />\r\n </
t:Paragraph.TabStops
>\r\n </
t:Paragraph
>\r\n <
t:Table
GridColumnWidthsSerializationInfo=\"\" LayoutMode=\"AutoFit\" PreferredWidth=\"Auto\" StyleName=\"TableGrid\" TableIndent=\"0\">\r\n <
t:TableRow
>\r\n <
t:TableCell
ColumnSpan=\"1\" RowSpan=\"1\">\r\n <
t:Paragraph
>\r\n <
t:Span
Text=\" { MERGEFIELD TmHeader } \" />\r\n </
t:Paragraph
>\r\n </
t:TableCell
>\r\n <
t:TableCell
ColumnSpan=\"1\" RowSpan=\"1\">\r\n <
t:Paragraph
/>\r\n </
t:TableCell
>\r\n <
t:TableCell
ColumnSpan=\"1\" RowSpan=\"1\">\r\n <
t:Paragraph
/>\r\n </
t:TableCell
>\r\n </
t:TableRow
>\r\n <
t:TableRow
>\r\n <
t:TableCell
ColumnSpan=\"1\" RowSpan=\"1\">\r\n <
t:Paragraph
/>\r\n </
t:TableCell
>\r\n <
t:TableCell
ColumnSpan=\"1\" RowSpan=\"1\">\r\n <
t:Paragraph
/>\r\n </
t:TableCell
>\r\n <
t:TableCell
ColumnSpan=\"1\" RowSpan=\"1\">\r\n <
t:Paragraph
/>\r\n </
t:TableCell
>\r\n </
t:TableRow
>\r\n </
t:Table
>\r\n <
t:Paragraph
TextAlignment=\"Left\">\r\n <
t:Paragraph.TabStops
>\r\n <
t:TabStop
Position=\"208\" TabStopType=\"Left\" />\r\n </
t:Paragraph.TabStops
>\r\n </
t:Paragraph
>\r\n </
t:Section
>\r\n</
t:RadDocument
>"