0

So I'm trying to create this program that works perfect in Visual Studio 2012 at School.

But when I'm at home it's giving me this Thrown Exception error at Public Game1().

Hopefully someone can help me out with this so that way i can actually run my program and finish it.

The Call Stack is:

System.NullReferenceException: Object reference not set to an instance of an object at Microsoft.Xna.Framework.OpenTKGameWindow.Initialize () [0x00000] in :0 at Microsoft.Xna.Framework.OpenTKGameWindow..ctor () [0x00000] in :0 at Microsoft.Xna.Framework.OpenTKGamePlatform..ctor (Microsoft.Xna.Framework.Game game) [0x00000] in :0 at Microsoft.Xna.Framework.GamePlatform.Create (Microsoft.Xna.Framework.Game game) [0x00000] in :0 at Microsoft.Xna.Framework.Game..ctor () [0x00000] in :0 at MonoGameTest.Game1..ctor () [0x00023] in /Users/sebnabt/Projects/MonoGameTest/MonoGameTest/Game1.cs:38 at MonoGameTest.Program.Main () [0x00001] in /Users/sebnabt/Projects/MonoGameTest/MonoGameTest/Program.cs:19

And my code:

<!-- language: lang-cs -->

region Using Statements
using System; 
using System.Collections.Generic; 
using System.Linq; 

using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Storage; 
using Microsoft.Xna.Framework.Input;
endregion

namespace MonoGameTest 
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Game
{
 GraphicsDeviceManager graphics;
 SpriteBatch spriteBatch;

 // Maps user keys to display colors
 Dictionary<Keys, Color> key_to_color = new Dictionary<Keys, Color>();

 // these are the colors guessed by the user, so far.
 List<Color>[] player_colors = new List<Color>[10];

 // these are the keys that correspond to a dot color
 List<Keys> color_letters = new List<Keys>();

 Texture2D dot_picture;

 KeyboardState old_keyboard_state;

 int row;        

 public Game1() : base()
 {
     graphics = new GraphicsDeviceManager(this);
     Content.RootDirectory = "Content";

     base.Initialize ();
 } 

 /// <summary>
 /// Allows the game to perform any initialization it needs to before starting to run.
 /// This is where it can query for any required services and load any non-graphic
 /// related content.  Calling base.Initialize will enumerate through any components
 /// and initialize them as well.
 /// </summary>
 /// 
 /// // screen constants
 const int SCREEN_WIDTH = 640;
 const int SCREEN_HEIGHT = 480;


 protected override void Initialize()
 {
     row = 0;
     // back buffer
     graphics.PreferredBackBufferHeight = SCREEN_HEIGHT;
     graphics.PreferredBackBufferWidth = SCREEN_WIDTH;
     graphics.PreferMultiSampling = false;
     graphics.ApplyChanges();
     base.Initialize();

     // initialize the color dictionary
     key_to_color.Add(Keys.R, Color.Red);
     key_to_color.Add(Keys.G, Color.Green);
     key_to_color.Add(Keys.B, Color.Blue);
     key_to_color.Add(Keys.Y, Color.Yellow);

     color_letters = new List<Keys>();
     foreach (Keys letter in key_to_color.Keys)
     {
        color_letters.Add(letter);
     }

  }

  /// <summary>
  /// LoadContent will be called once per game and is the place to load
  /// all of your content.
  /// </summary>
  protected override void LoadContent()
  {
    // Create a new SpriteBatch, which can be used to draw textures.
    spriteBatch = new SpriteBatch(GraphicsDevice);

    // load our textures
    dot_picture = Content.Load<Texture2D>(@"media\ball");
  }

  /// <summary>
  /// Allows the game to run logic such as updating the world,
  /// checking for collisions, gathering input, and playing audio.
  /// </summary>
  /// <param name="gameTime">Provides a snapshot of timing values.</param>
  protected override void Update(GameTime gameTime)
  {
    // get keyboard state once per frame
    KeyboardState keyState = Keyboard.GetState();

    // exit the game?
    if (keyState.IsKeyDown(Keys.Escape))
    {
        this.Exit();
    }

    // erase list of player colors?
    else if (keyState.IsKeyDown(Keys.Space))
    {
        player_colors[this.row].Clear();
    }

    // player pressed a color key?
    else {
        foreach (Keys letter in color_letters)
        {
            if (key_was_released(letter, keyState))
            {
                add_color(letter);  
            }
            if (this.player_colors [row].Count () >= 4) {
                this.row = this.row + 1;
            }
        }
    }



    old_keyboard_state = keyState;

    base.Update(gameTime);
}

protected override void UnloadContent() {

}

bool key_was_released(Keys key, KeyboardState new_state)
{
    return old_keyboard_state.IsKeyDown(key) && 
        new_state.         IsKeyUp  (key);
}

void add_color(Keys key)
{
    Color color = key_to_color[key];
    if (player_colors[this.row].Count() < 4)
    {
        player_colors[this.row].Add(color);
    }
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
    graphics.GraphicsDevice.Clear (Color.CornflowerBlue);

    // draw typical sprites
    spriteBatch.Begin ();
    draw_player_colors (spriteBatch);
    spriteBatch.End ();

    base.Draw (gameTime);
}

protected void draw_player_colors(SpriteBatch batch)
{
    for (int current_row = 0; current_row < 10; current_row++) {
        for (int column = 0; column < player_colors[current_row].Count(); column++) {
            int x = 100 + column * 70;
            int y = 100 + column * 70;
            Vector2 loc = new Vector2 (x, y);
            Color c = player_colors[current_row].ElementAt (column);
            batch.Draw (dot_picture, loc, c);
        }
    }
}
}

}

Newest Error:

It looks as though I have gotten it to slightly work however I'm now getting an error in the Basic Template on: logoTexture = Content.Load<Texture2D>("logo");

The error will be below

Microsoft.Xna.Framework.Content.ContentLoadException: Could not load logo asset as a non- content file! ---> System.Exception: The directory was not found. ---> System.Exception: Could not find a part of the path "/Users/sebnabt/Projects/BSMac/BSMac/bin/Debug/BSMac.app/Contents/Resources/Content/logo.xnb". at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x001c6] in /private/tmp/source/bockbuild-xamarin/profiles/mono-mac-xamarin-no-pcl/build-root/mono-3.2.0/mcs/class/corlib/System.IO/FileStream.cs:266 at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) [0x00000] in /private/tmp/source/bockbuild-xamarin/profiles/mono-mac-xamarin-no-pcl/build-root/mono-3.2.0/mcs/class/corlib/System.IO/FileStream.cs:132 at at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
at System.IO.File.OpenRead (System.String path) [0x00000] in /private/tmp/source/bockbuild-xamarin/profiles/mono-mac-xamarin-no-pcl/build-root/mono-3.2.0/mcs/class/corlib/System.IO/File.cs:341 at Microsoft.Xna.Framework.TitleContainer.OpenStream (System.String name) [0x00000] in :0 at Microsoft.Xna.Framework.Content.ContentManager.OpenStream (System.String assetName) [0x00000] in :0 --- End of inner exception stack trace --- at Microsoft.Xna.Framework.Content.ContentManager.OpenStream (System.String assetName) [0x00000] in :0 at Microsoft.Xna.Framework.Content.ContentManager.ReadAsset[Texture2D] (System.String assetName, System.Action1 recordDisposableObject) [0x00000] in <filename unknown>:0 --- End of inner exception stack trace --- at at Microsoft.Xna.Framework.Content.ContentManager.ReadAsset<Microsoft.Xna.Framework.Graphics.Texture2D> (string,System.Action1) <0x0076b> at at Microsoft.Xna.Framework.Content.ContentManager.Load (string) <0x0020b> at BSMac.Game1.LoadContent () [0x00023] in /Users/sebnabt/Projects/BSMac/BSMac/Game1.cs:70 at at Microsoft.Xna.Framework.Game.Initialize () at BSMac.Game1.Initialize () [0x00002] in /Users/sebnabt/Projects/BSMac/BSMac/Game1.cs:57 at at Microsoft.Xna.Framework.Game.DoInitialize ()
at at Microsoft.Xna.Framework.Game.Run (Microsoft.Xna.Framework.GameRunBehavior) at at Microsoft.Xna.Framework.Game.Run () at BSMac.AppDelegate.FinishedLaunching (MonoMac.Foundation.NSObject) [0x00012] in /Users/sebnabt/Projects/BSMac/BSMac/Main.cs:34 at at (wrapper dynamic-method) object.[BSMac.AppDelegate.Void FinishedLaunching(MonoMac.Foundation.NSObject)] (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector,MonoMac.Foundation.NSObject) at at (wrapper native-to-managed) object.[BSMac.AppDelegate.Void FinishedLaunching(MonoMac.Foundation.NSObject)] (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector,MonoMac.Foundation.NSObject) at at (wrapper managed-to-native) MonoMac.AppKit.NSApplication.NSApplicationMain (int,string[]) <0x00003> at at MonoMac.AppKit.NSApplication.Main (string[]) at BSMac.Program.Main (string[]) [0x0001d] in /Users/sebnabt/Projects/BSMac/BSMac/Main.cs:20 at

1 Answer 1

0

Are you building in Xamarin Studio? I believe you are experiencing the same problem described in a question from a couple days ago; see my answer there: https://stackoverflow.com/a/18798311/793449

To recap, Xamarin Studio is picking the WindowsGL framework assembly instead of the mac assembly. You should build MonoGame yourself from the latest source (the MonoGame.Framework.MacOS project) and change the reference out in your game.

7
  • I know this is a different topic, but is there a way you could give me a step by step guide so i can get this work? Sep 15, 2013 at 0:04
  • It looks as though i have gotten it to slightly work however i'm now getting an error in the Basic Template on: logoTexture = Content.Load<Texture2D>("logo"); The error will be added to the statement Sep 15, 2013 at 0:13
  • You need to right-click that file Content/logo.png in your project explorer, and change its build action to 'Content'. For some reason it is set to 'None' by default, so it is not included in the output. Then it can't find it at runtime. Sep 15, 2013 at 1:48
  • I've done that but i'm still getting the error that it can't load Sep 17, 2013 at 0:59
  • The error is very clear that it can't find the file in the expected location, which is in Contents/Resources/Content/ inside the built App package. You should be able to confirm this by manually going into the built app package, creating that path, and copying in logo.png yourself. At least, I have verified this locally using a clean Xam.Studio / MonoGame environment I setup 3 days ago. As for why setting it to 'Content' in the project isn't working, maybe it's as simple as cleaning the project and trying again? Sep 17, 2013 at 3:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.