Hi guys, I'm in need of some help here. I'm working on a music player in which I have a listView that populates with all the MP3 files in a certain directory. However, my goal now is to write this to an XML database so I don't have to keep populating it from the directory and rereading the MP3 tags, etc.
I've setup a class that's as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace MusicPlayer.Classes
{
class MusicLibrary
{
public string artist { get; private set; }
public string album { get; private set; }
public string song { get; private set; }
public string genre { get; private set; }
public string fileLocation { get; private set; }
public MusicLibrary(string Artist, string Album, string Song, string Genre, string FileLocation)
{
artist = Artist;
album = Album;
song = Song;
genre = Genre;
fileLocation = FileLocation;
}
public override string ToString()
{
return artist;
}
public void writeXml()
{
XDocument doc = new XDocument(
new XElement("MusicLibrary",
new XElement("Track", song),
new XAttribute("Artist", album),
new XAttribute("Genre", artist),
new XAttribute("Album", genre),
new XAttribute("FileLocation", fileLocation)
)
);
doc.Save("Library.xml");
}
}
}
This contains all the different types I'd want to write to the XML and a saving method. In my code, I've setup a foreach loop to go through each of the listViewItems, get the string of the data, and pass it into the writeXML method. It is as follows:
private void button4_Click(object sender, EventArgs e)
{
foreach(ListViewItem lvii in listView1.Items)
{
try
{
string finalArtist = lvii.Text;
string finalAlbum = lvii.SubItems["Album"].Text;
string finalSong = lvii.SubItems["Song Title"].Text;
string finalGenre = lvii.SubItems["Genre"].Text;
string finalLoc = lvii.SubItems["File Name"].Text;
MusicLibrary ml = new MusicLibrary(finalArtist, finalAlbum, finalSong, finalGenre, finalLoc);
ml.writeXml();
}
catch (NullReferenceException ex)
{
Console.WriteLine(ex.Message);
}
}
}The goal would be for it to still write the string as blank even if it doesn't contain a tag (or even as "Unknown Artist", "Unknown Album", etc). However, when calling the class with the following in the listView:

It calls a NullReferenceException at the "string finalAlbum = lvii.SubItems["Album"].Text;" line.
Exception Detail:
System.NullReferenceException was unhandledHResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=MusicPlayer
StackTrace:
at MusicPlayer.MainForm.button4_Click(Object sender, EventArgs e) in C:\Users\Mike\Documents\Visual Studio 2010\Projects\MusicPlayer\MusicPlayer\MainForm.cs:line 268
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at MusicPlayer.Program.Main() in C:\Users\Mike\Documents\Visual Studio 2010\Projects\MusicPlayer\MusicPlayer\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Any ideas on what could be causing this? Or if I'm even doing any of this right? I'm still rather new to XML. Any and all help would be appreciated
Thanks,
Mike