"An unhandled exception of type 'System.TypeInitializationException' occurred in Game.exe" Is the error I receive when I try run my program. It says it builds with 0 errors, but it won't launch, and when I run it in debug that is the message I get.
I have figured it is something do with this class:
class Sprites
{
private static Bitmap sheet;
private const int TILE_SIZE = 32;
public static Bitmap loadSprite(string file)
{
Bitmap sprite;
sprite = new Bitmap("Content/Sprites/" + file);
return sprite;
}
public static Bitmap getSprite(int x, int y)
{
if (sheet == null)
sheet = loadSprite("char.png");
Rectangle crop = new Rectangle(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
System.Drawing.Imaging.PixelFormat format = sheet.PixelFormat;
return sheet.Clone(crop, format);
}
}
It seems to be when the methods inside it are called because when I run without calling anything in this class it is ok. I have nothing else in my main form as I have just started this.
Now I've tried using a bitmap file which didn't do any good. It errors on the initialization line in program.cs "Application.Run(new Form1());". It doesn't tell me more than that. I posted my class because it runs when I don't use it.
Inside form1.cs
static Bitmap[] walkForward = { Sprites.getSprite(2, 0), Sprites.getSprite(2, 1), Sprites.getSprite(2, 2) };
Animation walking = new Animation(walkForward, 10);
public Form1()
{
InitializeComponent();
}
Full Error output to text file:
System.TypeInitializationException: The type initializer for 'Game.Form1' threw an exception. ---> System.ArgumentException: Parameter is not valid. at System.Drawing.Bitmap..ctor(String filename) at Game.Sprites.loadSprite(String file) in c:\Users\tiern\Documents\Visual Studio 2013\Projects\MoveSpites\Game\Sprites.cs:line 19 at Game.Sprites.getSprite(Int32 x, Int32 y) in c:\Users\tiern\Documents\Visual Studio 2013\Projects\MoveSpites\Game\Sprites.cs:line 27 at Game.Form1..cctor() in c:\Users\tiern\Documents\Visual Studio 2013\Projects\MoveSpites\Game\Form1.cs:line 15 --- End of inner exception stack trace --- at Game.Form1..ctor() at Game.Program.Main() in c:\Users\tiern\Documents\Visual Studio 2013\Projects\MoveSpites\Game\Program.cs:line 21
System.ArgumentException: Parameter is not valid. at System.Drawing.Bitmap..ctor(String filename) at Game.Sprites.loadSprite(String file) in c:\Users\tiern\Documents\Visual Studio 2013\Projects\MoveSpites\Game\Sprites.cs:line 19 at Game.Sprites.getSprite(Int32 x, Int32 y) in c:\Users\tiern\Documents\Visual Studio 2013\Projects\MoveSpites\Game\Sprites.cs:line 27 at Game.Form1..cctor() in c:\Users\tiern\Documents\Visual Studio 2013\Projects\MoveSpites\Game\Form1.cs:line 15