I am creating a custom management pack for Service Manager and want to add related items between the custom config items. I am getting a null reference exception and not sure where I have gone wrong, any input would be appreciated. The error is:
Application: Microsoft.EnterpriseManagement.ServiceManager.UI.Console.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
at APOSystemAccessForms.Location.AddItemToListView(System.Windows.Controls.ListView, System.Guid)
at APOSystemAccessForms.Location.btnAddBusinessUnit_Click(System.Object, System.Windows.RoutedEventArgs)
I have debugged the code, the error is occurring at line 23 or maybe 24 and the debugger lists the listView.ItemSource as null.
internal static void AddItemToListView(ListView listView, Guid classId)
{
if (listView != null && listView.Items != null)
{
/* NOTE: The use of the IDataItem and InstancePickerDialog interfaces here is not supported/documented.
* This interface may change in the future and no migration path is guaranteed by Microsoft.
*/
InstancePickerDialog ciPicker = new InstancePickerDialog();
ciPicker.ClassId = classId;
ciPicker.SelectionMode = SelectionMode.Multiple;
if (listView.Items.Count > 0)
{
ciPicker.SetPickedInstances((Collection<IDataItem>)listView.ItemsSource);
}
bool? result = ciPicker.ShowDialog();
if (result != null && result == true)
{
Collection<IDataItem> items = listView.ItemsSource as Collection<IDataItem>;
foreach (IDataItem item in ciPicker.RemovedInstances)
items.Remove(item);
foreach (IDataItem item in ciPicker.PickedInstances)
if (!items.Contains(item))
items.Add(item);
}
}
}
Classes:
<ClassType ID="APO.Class.BusinessUnit" Base="System!System.ConfigItem" Accessibility="Public" Abstract="false" Hosted="false" Singleton="false" Extension="false">
<Property ID="BusinessUnitId" Key="true" AutoIncrement="true" Type="string" Required="true" DefaultValue="{0}" />
<Property ID="BusinessUnitMnemonic" Type="string" Required="true" />
</ClassType>
<ClassType ID="APO.Class.Location" Base="System!System.ConfigItem" Accessibility="Public" Abstract="false" Hosted="false" Singleton="false" Extension="false">
<Property ID="LocationId" Key="true" AutoIncrement="true" Type="string" Required="true" DefaultValue="{0}" />
<Property ID="LocationMnemonic" Type="string" Required="true" />
</ClassType>
Relationship:
<RelationshipType ID="APO.Relationship.LocationToBusinessUnit" Base="System!System.Reference" Accessibility="Public" Abstract="false">
<Source ID="APO.Source.LocationClass" Type="APO.Class.Location" MinCardinality="0" MaxCardinality="2147483647"/>
<Target ID="APO.Target.BusinessUnitClass" Type="APO.Class.BusinessUnit" MinCardinality="0" MaxCardinality="2147483647"/>
</RelationshipType>
Type projection:
<TypeProjection ID="APO.TypeProjection.LocationClass" Accessibility="Public" Type="APO.Class.Location">
<Component Path="$Target/Path[Relationship='APO.Relationship.LocationToBusinessUnit']$" Alias="RelatesToBusinessUnit" />
</TypeProjection>
Form reference:
<Form ID="APO.Form.Location" Accessibility="Public" Target="APO.TypeProjection.LocationClass" Assembly="Forms" TypeName="Forms.Location">
ListView:
<ListView Name="lvBusinessUnit" Height="150" ItemsSource="{Binding Path=RelatesToBusinessUnit, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">