【发布时间】: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。不过,您比我们更能解决这个问题。