[SOLVED] MediaPlayer Object reference not set to an instance of an object

It does not matter which Property or method I access of the MediaPlayer class in the mono framework I receive

Exception thrown: 'System.NullReferenceException' in MonoGame.Framework.dll
Additional information: Object reference not set to an instance of an object.

Stacktrace

at Microsoft.Xna.Framework.Media.MediaPlayer.PlatformInitialize()

Platform: Universal Windows

Why are you using static methods here? If the Music class constructor is called before MediaPlayer, then MP will be null. At least move that initialization into the LoadContentAudio method. Or simply turn the class into non-static.

[quote=“KuzDeveloper, post:2, topic:8179, full:true”]
Why are you using static methods here?[/quote]

Because I have one static class to control the music in the game.

The static constructor will be called when the first call on the static class takes place. And thats in LoadContentAuto. Which will be invoked (reflection) after the LoadContent in the game.cs

protected override void LoadContent()
{
    SpriteBatch = new SpriteBatch(GraphicsDevice);
    ScreenManager.AddScreen(new Loading());
}

public class Loading : Screen
{
    private EntityManager manager = new EntityManager();

    public Loading()
    {
        manager += Entities.Information.CreateLoadingInformation();
        Task.Factory.StartNew(StartLoadingContent);
    }

    private void StartLoadingContent()
    {
        List<Type> methods = GetType().GetTypeInfo().Assembly.GetTypes().Where(t => t.GetMethods().ToList().Where(m => m.Name == "LoadContentAuto").Count() > 0).ToList();
        foreach (Type currentMethod in methods)
                currentMethod.GetMethod("LoadContentAuto").Invoke(null, new object[1] { ResourceManager.ApplicationResources });

        Game.Singleton.Components.Add(new PerformanceManager(Game.Singleton));
        ScreenManager.AddScreen(new SceneSwitchScreen(null, new Town()));
        RemoveScreen();
    }

public static class Music
{
    private static Dictionary<Title, Song> songDictionary = new Dictionary<Title, Song>();

    static Music()
    {
        MediaPlayer.MediaStateChanged += MediaPlayer_MediaStateChanged;
    }

    public static void LoadContentAuto(ContentManager content)
    {
        songDictionary.Add(Title.Town, content.Load<Song>(@"Music/Town"));
    }

Checked the sourcecode of monogame. LoadContent will be called after Initialize. So it should be fine, right?

At the callstack in VS I get:
LoadContent -> LoadContentAuto -> static constructor

I dont know why it is working, I removed the static constructor and added a function called “Initialize” which does the same as the static constructor before. That works somehow.

protected override void LoadContent()
{
    base.LoadContent();

    SpriteBatch = new SpriteBatch(GraphicsDevice);
    ScreenManager.AddScreen(new Loading());
    Music.Initialize();
}