-2

"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

Tiernan Watson
  • 313
  • 2
  • 12
  • What is the message of that exception? – Lasse V. Karlsen Nov 29 '15 at 18:42
  • You need to expand the InnerException in the exception details to get the full error. – Cyral Nov 29 '15 at 18:45
  • what about sheet.. have you tried to do the following `sheet = new Bitmap();` set some breakpoints and tell us what line it errors out on .. – MethodMan Nov 29 '15 at 18:47
  • What is the stack trace? Do you have a static constructor? Or are you initializing any static members inline? – Yacoub Massad Nov 29 '15 at 18:49
  • Updated the main question with extra info. Not sure why you would need a static constructor? – Tiernan Watson Nov 29 '15 at 19:00
  • The code as shown would not produce any initialization exception because you're not initializing anything in the type initializer (aka static constructor) because you don't have a type initializer nor do you have any initialization expressions for the static fields. Can you please show the full details of the exception, its message, any inner exceptions and their messages, stack traces, etc.? Is this the complete class by the way, or did you remove bits of it before posting? Please consider writing a [mcve]. – Lasse V. Karlsen Nov 29 '15 at 19:02
  • Sorry, I had never done this. Updated with inner exceptions. Also, no this is the full class. Animation and frames are handled in a separate class. – Tiernan Watson Nov 29 '15 at 19:18
  • So the problem was in a different class, but fails inside your sprite class when loading sprites. Most likely it cannot find the sprite file on disk. Try calculating the full correct path instead of just a relative path, also use Path.Combine to calculate the path from individual parts, and don't do string concatenation or use `/` as the path separator. – Lasse V. Karlsen Nov 29 '15 at 19:43
  • Thanks for the help! – Tiernan Watson Nov 29 '15 at 20:15
  • 1
    I would recommend that you consider the `static` keyword as non-existent, that is, refrain from using it as if its very presence in the source code would cause a syntax error. When you become a veteran programmer one day, you can suddenly discover it. – Mike Nakis Nov 29 '15 at 20:22

1 Answers1

0

Turns out it could not find the file on the disk. It reads from the bin folder and not the root of project.

Tiernan Watson
  • 313
  • 2
  • 12