Исключение нулевого указателя генерируется, когда приложение пытается использовать null в случае, когда требуется объект. К ним относятся:
null
. null
. null
, как если бы это был массив. null
, как если бы это был массив. null
как будто это было значение Throwable. Приложения должны бросать экземпляры этого класса, чтобы указать на другие незаконные использования объекта null
.
Ссылка: http://docs.oracle.com/javase/8/docs/api/java/lang/NullPointerException.html
Вот пример, взятый от Вопрос о MSDN , общественность освобождает ScrollToItem (международный индекс)
{
Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background,
(System.Windows.Threading.DispatcherOperationCallback)delegate(object arg)
{
int N = fileList.Items.Count;
if (N == 0)
return null;
if (index < 0)
{
fileList.ScrollIntoView(fileList.Items[0]); // scroll to first
}
else
{
if (index < N)
{
fileList.ScrollIntoView(fileList.Items[index]); // scroll to item
}
else
{
fileList.ScrollIntoView(fileList.Items[N - 1]); // scroll to last
}
}
return null;
}, null);
}
Я использовал прикрепленное свойство для решения этой проблемы.
public class TreeViewItemBehaviour
{
#region IsBroughtIntoViewWhenSelected
public static bool GetIsBroughtIntoViewWhenSelected(TreeViewItem treeViewItem)
{
return (bool)treeViewItem.GetValue(IsBroughtIntoViewWhenSelectedProperty);
}
public static void SetIsBroughtIntoViewWhenSelected(
TreeViewItem treeViewItem, bool value)
{
treeViewItem.SetValue(IsBroughtIntoViewWhenSelectedProperty, value);
}
public static readonly DependencyProperty IsBroughtIntoViewWhenSelectedProperty =
DependencyProperty.RegisterAttached(
"IsBroughtIntoViewWhenSelected",
typeof(bool),
typeof(TreeViewItemBehaviour),
new UIPropertyMetadata(false, OnIsBroughtIntoViewWhenSelectedChanged));
static void OnIsBroughtIntoViewWhenSelectedChanged(
DependencyObject depObj, DependencyPropertyChangedEventArgs e)
{
TreeViewItem item = depObj as TreeViewItem;
if (item == null)
return;
if (e.NewValue is bool == false)
return;
if ((bool)e.NewValue)
{
item.Loaded += item_Loaded;
}
else
{
item.Loaded -= item_Loaded;
}
}
static void item_Loaded(object sender, RoutedEventArgs e)
{
TreeViewItem item = e.OriginalSource as TreeViewItem;
if (item != null)
item.BringIntoView();
}
#endregion // IsBroughtIntoViewWhenSelected
}
А в моем стиле XAML для TreeViewItem я просто установил для свойства значение true
<Setter Property="Behaviours:TreeViewItemBehaviour.IsBroughtIntoViewWhenSelected" Value="True" />
HTH