Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
400
NullReferenceException in DataPresenter.Calculations.RefUtils.GetRecord when sorting
posted

Hello,

I am using a XamDataGrid with a few calculated unbound fields.  I am using 2012.1 build 2107.  LabelClickAction is SortByMultipleFieldsTriState but I am clicking on only a single column header to cause the NullReferenceException.  Initial sort description is Field "Group" with IsGroupBy="True".  "Group" is an UnboundField that is Collapsed and there is a GroupByEvaluator and GroupByComparer.  I had to make some changes to the GroupByEvaluator and GroupByComparer to work around a bug in the DataPresenterExcelExporter.Export. 

Line 8181 of Infragistics.Windows.DataPresenter.Calculations.RefUtils.GetRecord throws the exception because records is NULL:
MainRecordSparseArray arr = records.SparseArray as MainRecordSparseArray;

I will describe why records is NULL here.  Look at the attached call tree text file for reference.
Evaluate(long ticks) enters a while loop on line 1912 and calls EvaluteNextRow() on line 1914 in UltraCalcEngine\UltraCalcEngine.cs
EvaluteNextRow() calls MoveNext() on line 1731 in UltraCalcEngine\Utils.cs
the first line of MoveNext() calls _recordIterator.MoveNext() on line 6144 in DataPresenterCalculationAdapter\References.cs
_recordIterator._child is not NULL and _recordIterator._firstTime is true - do loop is entered at line 5868 in References.cs
scopeResolved is set to Any on line 5914, _childTuple.Name is equal to the Field I clicked on to sort by, _childTuple.Scope is Any
this.GetFirstRecord() is called on line 5944
In GetFirstRecord(), _calcScope is FilteredSortedList, _records is a DataRecordCollection with _groupByField = NULL, _previousSortStatus = NotSorted, IsRootLevel = true, IsTopLevel = true, RecordsType = DataRecord
_rootRef._utils.GetRecord( _records, 0, calcScope ) is called on line 5782
in GetRecord, arr.GetItemAtVisibleIndex( calcIndex ) is called on line 8183, value of call is returned down call frame as value for GetFirstRecord() call on line 5944 of References.cs in RefRecordIterator.MoveNext()
_record is set to return value and has ViewableChildRecords = collection with 0 records, HasChildData = false, HasChildren = false, ParentRecord = GroupByRecord with 95 items
_child has field values: _child = null, _childTuple = (name of column with formula), _record = null, _tuple = (name of column I clicked on to sort)
RefUtils.GetImmediateChildRecordsMatchingFieldLayout( _record, _child._fieldLayout ) is called on line 6047 of References.cs
childRecords is set to dr.ChildRecords which is a ExpandableFieldRecordCollection with Count 0
NULL is returned since the collection has no child records - return value of NULL is set to _childRecords on line 6047 of References.cs
_child.InitRecordsCollection( childRecords ) is called on the next line 6048 - childRecords is NULL
InitRecordsCollection sets _records field to childRecords (which is NULL).  It then calls this.Reset() which sets _record to NULL and _firstTime to true
After it returns the while instruction is executed - while ( null != _record && null != _child && !_child.MoveNext( ) ); - _record is not null and _child is not null so _child.MoveNext() is called
In _child.MoveNext(), _child is NULL, _firstTime is true, _tuple.Scope is Any, _childTuple.Name = (name of unbound field with formula)
_record = this.GetFirstRecord( ); is called at line 5944
_rootRef._utils.GetRecord( _records, 0, calcScope ) is called, from GetFirstRecord() with _records = null, _calcScope = FilteredSortedList on line 5789
In GetRecord, which was called with null _records, this line generates an uncaught NullReferenceException:  
MainRecordSparseArray arr = records.SparseArray as MainRecordSparseArray; 

I have attached a text file with the call tree when the exception is thrown along with line numbers since I downloaded the source and pdb's for the build.  The exception occurs when attempting to call MoveNext() on an child collection which is empty.  The _record field in the child collection is NULL.  Evaluate(long ticks) calls EvaluteNextRow() which calls MoveNext() to get the first row of the data presenter.  Now where this gets strange is the RefRecordIterator thinks that the first row has a child collection.  The exception occurs because when evaluating a simple unbound field reference with a formula, it creates an empty child collection with a NULL collection pointer and then attempts to get the SparseArray from the NULL reference.

Let me know if there is any other information you need.  The formula attempting to be calculated is a simple subtraction of an unbound bound field (with a binding path) from a bound field.