Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#1.0 C#2.0 C#3.0 C#4.0
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:
 
        // -- 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:
        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):
    [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;
        }
    }
 
    [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 24-Dec-12 11:06am
Yvar Birx2.4K
Comments
Sergey Alexandrovich Kryukov at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Serializers require a default constructor on types for deserialization i.e. :
    [Serializable]
    public class Chunk
    {
          public Chuck()
          {
          }
...
  Permalink  
Comments
Yvar Birx at 25-Dec-12 6:40am
   
I already have placed that ?
Mehdi Gholam at 25-Dec-12 7:33am
   
The code you posted does not have it.
Yvar Birx at 25-Dec-12 13:01pm
   
Don't I have that ? If not, could you point it out to me ? :)
Yvar Birx at 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 at 25-Dec-12 14:15pm
   
Default constructors do not have parameters.
Yvar Birx at 25-Dec-12 14:43pm
   
Alright, thanks. Last problem; It does not serialize the blocks, this is my result:
 
"
< chunk >
< x > 0 < /x >
< y > -1< / y >
< / chunk >
"
 
Where are my blocks at ?
 
Also I changed the code of the block class to:
 
" [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; }
 
public Block()
{
}
 
// -- Block
///
/// Initializes a new Block with the ID given.
///

/// <param name="_ID">The ID of the block.</param>
public void InitializeBlock(byte _ID)
{
// -- Set the ID.
ID = _ID;
}
}
"
Yvar Birx at 25-Dec-12 14:44pm
   
Also, Chunk class is similair but works.

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

  Print Answers RSS
0 OriginalGriff 310
1 Sergey Alexandrovich Kryukov 176
2 PhilLenoir 164
3 Richard MacCutchan 160
4 Sharmanuj 146
0 Sergey Alexandrovich Kryukov 6,081
1 OriginalGriff 5,115
2 CPallini 2,473
3 Richard MacCutchan 1,597
4 Abhinav S 1,505


Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 25 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100