0

I am making a simple music player implementing the WMPlib to play media files .... I am trying to open the file using a open file dialog ... the dialog comes and able to select the file but an exception comes when I try to assign the filename to Player.URL

at the line

Player.URL = openFileDialog1.FileName;

the error says

Object reference not set to an instance of an object. Can anyone please give me a clue on how to assign the filename to the player.URL

the complete code is as follows....

using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {

        WMPLib.WindowsMediaPlayer Player;
        public Form1()
        {
            InitializeComponent();
        }




        private void PlayFile(String url)
        {
            Player = new WMPLib.WindowsMediaPlayer();
            Player.PlayStateChange +=
                new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(Player_PlayStateChange);
            Player.MediaError +=
                new WMPLib._WMPOCXEvents_MediaErrorEventHandler(Player_MediaError);
            Player.URL = url;
            Player.controls.play();
        }

        private void Player_PlayStateChange(int NewState)
        {
            if ((WMPLib.WMPPlayState)NewState == WMPLib.WMPPlayState.wmppsStopped)
            {
                this.Close();
            }
        }

        private void Player_MediaError(object pMediaObject)
        {
            MessageBox.Show("Cannot play media file.");
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "(mp3,wav,mp4,mov,wmv,mpg)|*.mp3;*.wav;*.mp4;*.mov;*.wmv;*.mpg|all files|*.*";
            openFileDialog1.ShowDialog(); 



        }

        private void button2_Click(object sender, EventArgs e)
        {
            PlayFile(Player.URL);
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            MessageBox.Show(openFileDialog1.FileName);
            Player.URL = openFileDialog1.FileName;
        }
    }
}
Rishabh
  • 3,752
  • 4
  • 47
  • 74
  • Have a look here: http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net – default Jan 10 '13 at 07:43
  • With that said, how about keeping a field in your class `private string filename;` which you set in `button1_Click` as `filename = openFileDialog1.FileName;` and then read from in `openFileDialog1_FileOk` as `Player.URL = filename;`. Would that work for you? – default Jan 10 '13 at 07:45
  • the file path is being shown in the messagebox which is stored in openFileDialog1.FileName .... but I am not able to assign it to Player.URL – Rishabh Jan 10 '13 at 07:49
  • no that will give the same error – Rishabh Jan 10 '13 at 07:56
  • Then I guess `Player` hasn't been assigned. Would that be correct? Perhaps you could `new` that up in the constructor – default Jan 10 '13 at 08:14

2 Answers2

1

Try to use openFileDialog1 like this:

DialogResult result = openFileDialog1.ShowDialog();

if (result == DialogResult.OK)
{
   Player.URL = openFileDialog1.FileName;
}

in button1_Click()

Guy P
  • 1,395
  • 17
  • 33
  • please put break point at this line and tell me whats inside the "FileName" and "URL" – Guy P Jan 10 '13 at 07:45
  • @Rishabh on what line does that error display? the first one or the `Player.URL...`? – default Jan 10 '13 at 07:46
  • on Player.URL = openFileDialog1.FileName; – Rishabh Jan 10 '13 at 07:55
  • 1
    replace this line "Player.URL = openFileDialog1.FileName" with "PlayFile(openFileDialog1.FileName)" – Guy P Jan 10 '13 at 08:02
  • **please follow this link**: [link](http://msdn.microsoft.com/en-us/library/bb383953(v=vs.90).aspx) – Guy P Jan 10 '13 at 08:19
  • @Rishabh You need to tell us what object is `null`. Could you debug your application and check on what line you get the `NullReferenceException` and then what object is `null`? – default Jan 10 '13 at 08:21
1

Make sure that you create an instance of WMPLib.WindowsMediaPlayer before using it. Right now it seems that you are clicking 'open file' button and trying to assign returned file name to null object.

krajew4
  • 789
  • 4
  • 9
  • WMPLib.WindowsMediaPlayer Player; – Rishabh Jan 10 '13 at 08:09
  • Yes, but you create an instance of it in PlayFile method. Also, use following construction: if (openFileDialog.ShowDialog() == DialogResult.OK) { ... assign url to player ... } – krajew4 Jan 10 '13 at 08:11