如何解决未设置为对象实例的对象引用? [重复]

时间:2021-07-05 16:55:41

This question already has an answer here:

这个问题在这里已有答案:

In my c# MVVM program. I set one observable collections. And I add to the image from the list. But i get Object reference not set to an instance of an object error.

在我的c#MVVM程序中。我设置了一个可观察的集合。然后我从列表中添加图像。但我得到的Object引用未设置为对象错误的实例。

   private ObservableCollection<SpecialAttractionImageModel> _SpecialAttractionImageModelObj = new ObservableCollection<SpecialAttractionImageModel>();

    public ObservableCollection<SpecialAttractionImageModel> SpecialAttractionImageModelObj
            {
                get { return _SpecialAttractionImageModelObj; }
                set
                {
                    _SpecialAttractionImageModelObj = value;
                    OnPropertyChanged("SpecialAttractionImageModelObj");
                }
            }

foreach (String file in fd.FileNames)
                    {
                        FileInfo currentFile = new FileInfo(file);
                        _SpecialAttractionImageModelObj.Add(new SpecialAttractionImageModel()
                        {
                            AttractionImageGid = "new",
                            SpecialAttracionImagePath = file,
                            IsChecked=false
                        });
                    }

1 个解决方案

#1


-1  

Object is not initialized, maybe OnPropertyChanged("SpecialAttractionImageModelObj"); is the cause.

对象未初始化,可能是OnPropertyChanged(“SpecialAttractionImageModelObj”);是原因。

Stack Trace would help you figure out where this is happening.

Stack Trace可以帮助您找出发生这种情况的方法。

#1


-1  

Object is not initialized, maybe OnPropertyChanged("SpecialAttractionImageModelObj"); is the cause.

对象未初始化,可能是OnPropertyChanged(“SpecialAttractionImageModelObj”);是原因。

Stack Trace would help you figure out where this is happening.

Stack Trace可以帮助您找出发生这种情况的方法。