【问题标题】:Error: exception of type 'System.NullReferenceException' occurred in exe错误:exe 中出现“System.NullReferenceException”类型的异常
【发布时间】:2012-01-24 16:13:30
【问题描述】:

G'day 伙计们

每当我运行此程序时,我都会不断收到上述错误,并问我为什么。

我做了一个 Step Into,发现当我将对象添加到集合时发生了异常(在下面的代码中标记)。关于可能导致此问题的任何想法?

Img 和 Category 类是具有 inotify 接口的普通 ol 类,而 Movies 类具有 Observable 集合接口。

无论如何,这是有问题的代码...

        public void LoadMovieLibrary( string libraryfile , Movies obj)
    {
        try
        {
            var libraryXML = XElement.Load( libraryfile );
            if ( libraryXML != null )
            {
                IEnumerable<XElement> movies = from element in libraryXML.Descendants( "movie" ) select element;
                foreach ( XElement movie in movies )
                {
                    ObservableCollection<Category> categoryGroup = new ObservableCollection<Category>();
                    IEnumerable<XElement> categories = from element in movie.Descendants( "category" ) select element;
                    foreach ( XElement category in categories )
                    {
                        categoryGroup.Add(
                            new Category(
                                int.Parse( category.Attribute( "id" ).Value ) ,
                                category.Attribute( "name" ).Value
                                )
                            );
                    }

                    ObservableCollection<Img> imgGroup = new ObservableCollection<Img>();
                    IEnumerable<XElement> imgs = from element in movie.Descendants( "image" ) select element;
                    foreach ( XElement img in imgs )
                    {
                        imgGroup.Add(
                            new Img(
                                img.Attribute( "type" ).Value ,
                                img.Attribute( "url" ).Value
                                )
                            );
                    }
                    try
                    {
                        obj.Add( // <= this is where it breaks
                            new Movie(
                                movie.Element( "name" ).Value ,
                                int.Parse( movie.Element( "id" ).Value ) ,
                                movie.Element( "imdbid" ).Value ,
                                movie.Element( "overview" ).Value ,
                                movie.Element( "tagline" ).Value ,
                                movie.Element( "released" ).Value ,
                                int.Parse( movie.Element( "runtime" ).Value ) ,
                                movie.Element( "trailer" ).Value ,
                                categoryGroup ,
                                imgGroup ,
                                movie.Element( "filename" ).Value
                                )
                            );
                    }
                    catch ( Exception exception )
                    {
                        MessageBox.Show( exception.Message , "Error" , MessageBoxButton.OK , MessageBoxImage.Error );
                    }
                }
            }
        }
        catch ( Exception exception )
        {
            MessageBox.Show( exception.Message , "Error" , MessageBoxButton.OK , MessageBoxImage.Error );
        }
    }

编辑:

非常感谢

原来我在...上遗漏了一个下划线

  • movie.Element("imdbid").Value

应该是这样的

  • movie.Element("imdb_id").Value

【问题讨论】:

  • 我猜 xml 文件没有预期的元素之一(因此您正在对 null 执行 .Value)。那或obj。不过,您比我们更能解决这个问题。

标签: c# wpf xml class xelement


【解决方案1】:

每当您尝试使用空引用执行某些操作(例如调用方法、访问属性或字段等)时,都会抛出 NullReferenceException。在您的情况下,这意味着以下事情之一为空:

  • obj
  • movie
  • movie.Element( "name" )
  • movie.Element( "id" )
  • movie.Element( "imdbid" )
  • movie.Element( "overview" )
  • movie.Element( "tagline" )
  • movie.Element( "released" )
  • movie.Element( "runtime" )
  • movie.Element( "trailer" )
  • movie.Element( "filename" )

即您访问该行的属性或方法的所有内容。

在调试器中测试它们,看看哪个是空的。

【讨论】:

  • 非常感谢。原来我在movie.Element(“imdbid”)中遗漏了一个下划线。它应该是 movie.Element("imdb_id")。我现在觉得自己像个白痴:X
猜你喜欢
  • 1970-01-01
  • 2018-07-05
  • 2016-07-02
  • 1970-01-01
  • 2014-06-03
  • 1970-01-01
  • 2021-10-10
  • 1970-01-01
  • 2017-06-30
相关资源
最近更新 更多