Click here to Skip to main content
15,886,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Codeproject,

I am currently attempting to serialize a piece of data. But all my application is doing is crashing itself with:

"The type initializer for 'Terrasylum.Terrasylum' threw an exception."
No details are given, and the last thing I added was:

C#
// -- Create the Chunk.
/// <summary>
/// Creates the Chunk at the Cooardinate and returns the string..
/// </summary>
/// <param name="?"></param>
private string CreateChunk(int ChunkX, int ChunkY)
{
    // -- Create a stream writer.
    StreamWriter _Writer = new StreamWriter(Path + ChunkX + "_" + ChunkY + ".txt");

    // -- Create a new Chunk.
    Chunk _Chunk = new Chunk(ChunkX, ChunkY, ChunkSize, ChunkSize);

    // -- Generate the Chunk.
    for (int x = 0; x < ChunkSize; x++)
    {
        for (int y = 0; y < ChunkSize; y++)
        {
            // -- Block.
            byte Block = ID.BLOCK_AIR;
            if (ChunkY == 0 && y > ChunkSize / 2)
                Block = ID.BLOCK_GRASS;

            // -- Set the Block.
            _Chunk.SetBlockID(x, y, Block);
        }
    }

    // -- Get the XML string.
    string _ReturnString;
    _ReturnString = SerializeToString(_Chunk);

    // -- Write.
    _Writer.Write(_ReturnString);

    // -- Close the Writer.
    _Writer.Flush();
    _Writer.Close();

    // -- Return.
    return _ReturnString;
}


This is how I serialize:
C#
public static T SerializeFromString<T>(string xml)
{
    XmlSerializer serializer = new XmlSerializer(typeof(T));

    using (StringReader reader = new StringReader(xml))
    {
        return (T)serializer.Deserialize(reader);
    }
}


public static string SerializeToString(object obj)
{
    XmlSerializer serializer = new XmlSerializer(obj.GetType());

    using (StringWriter writer = new StringWriter())
    {
        serializer.Serialize(writer, obj);

        return writer.ToString();
    }
}


And these are my (Chunk and Block class):
C#
[Serializable]
public class Chunk
{
    // -- Blocks.
    private Block[,] _Blocks;

    // -- Cooardinates.
    public int X { get; set; }
    public int Y { get; set; }

    // -- Chunk.
    /// <summary>
    /// Initializes a new Chunk based on the Width and Height given.
    /// </summary>
    /// <param name="Width"></param>
    /// <param name="Height"></param>
    public Chunk(int X, int Y, int Width, int Height)
    {
        // -- Create the Block List.
        _Blocks = new Block[Width, Height];

        // -- Create all the Blocks.
        for (int i = 0; i < Width; i++)
        {
            for (int j = 0; j < Height; j++)
            {
                // -- Create a new block.
                Block _Block = new Block(ID.BLOCK_AIR);

                // -- Add the block.
                _Blocks[i,j] = _Block;
            }
        }
    }

    // -- Set a Block.
    /// <summary>
    /// Sets the block in the Chunk.
    /// </summary>
    /// <param name="X">X of the block.</param>
    /// <param name="Y">Y of the block.</param>
    /// <param name="ID">The ID of the block.</param>
    public void SetBlockID(int X, int Y, byte ID)
    {
        // -- Set the Block.
        _Blocks[X, Y].ID = ID;
    }

    // -- Get a block.
    /// <summary>
    /// Returns the ID of the given block.
    /// </summary>
    /// <param name="X">The X of the Block.</param>
    /// <param name="Y">The Y of the Block.</param>
    public byte GetBlockID(int X, int Y)
    {
        // -- Return.
        return _Blocks[X, Y].ID;
    }
}


C#
[Serializable]
public static class ID
{
    public static int BlockSize = 32;

    public const byte BLOCK_AIR = 30;
    public const byte BLOCK_DIRT = 31;
    public const byte BLOCK_GRASS = 32;
}

[Serializable]
public class Block
{
    public byte ID { get; set; }

    // -- Block
    /// <summary>
    /// Initializes a new Block with the ID given.
    /// </summary>
    /// <param name="_ID">The ID of the block.</param>
    public Block(byte _ID)
    {
        // -- Set the ID.
        ID = _ID;
    }
}



Could anyone help me out ? If I get this fixed I can finally move on. :(
Posted
Comments
Sergey Alexandrovich Kryukov 25-Dec-12 1:31am    
You've been asked to present an exception stack and inner exceptions. Do it recursively. Instead, you repeated that there is no error information. Exception is exception. It does not have to be "error", but you need to dump it in full. It has the type, message, parameters, inner exception and exception stack trace. Look at it. Provide all relevant information about it. Importantly, find out the line where the exception is thrown and mark it in your code.
—SA

1 solution

Serializers require a default constructor on types for deserialization i.e. :
C#
    [Serializable]
    public class Chunk
    {
          public Chuck()
          {
          }
...
 
Share this answer
 
Comments
Yvar Birx 25-Dec-12 6:40am    
I already have placed that ?
Mehdi Gholam 25-Dec-12 7:33am    
The code you posted does not have it.
Yvar Birx 25-Dec-12 13:01pm    
Don't I have that ? If not, could you point it out to me ? :)
Yvar Birx 25-Dec-12 13:02pm    
It's in:

"[Serializable]
public class Chunk
{
// -- Blocks.
private Block[,] _Blocks;

// -- Cooardinates.
public int X { get; set; }
public int Y { get; set; }

// -- Chunk.
///
/// Initializes a new Chunk based on the Width and Height given.
///

/// <param name="Width"></param>
/// <param name="Height"></param>
public Chunk(int X, int Y, int Width, int Height)
{"
Mehdi Gholam 25-Dec-12 14:15pm    
Default constructors do not have parameters.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900