G'day folks
I keep getting the error above whenever I run this and buggered me as to why.
I did a Step Into and found the exception occurs when I hit adding the object to the collection (marked in the code below). Any Ideas as to what may be causing this?
The Img and Category classes are plain ol classes with inotify interface, and the Movies class has an Observable collection Interface.
Anyways, here is the offending code...
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 );
}
}
Edit:
Many thanks
Turns out I left out an underscore on...
and it should of been
A NullReferenceException
is thrown whenever you attempt to do something with a null reference, for example call a method, access a property or field etc. In your case it means that one of the following things is null:
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" )
I.e. all of the things that you access properties or methods of on that line.
Test them all in the debugger to see which is null.