Click here to Skip to main content
Sign Up to vote bad
good
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:06
Yvar Birx2.2K

Comments
Sergey Alexandrovich Kryukov - 25 Dec '12 - 1:31
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. :
    [Serializable]
    public class Chunk
    {
          public Chuck()
          {
          }
...
  Permalink  
Comments
Yvar Birx - 25 Dec '12 - 6:40
I already have placed that ?
Mehdi Gholam - 25 Dec '12 - 7:33
The code you posted does not have it.
Yvar Birx - 25 Dec '12 - 13:01
Don't I have that ? If not, could you point it out to me ? :)
Yvar Birx - 25 Dec '12 - 13:02
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:15
Default constructors do not have parameters.
Yvar Birx - 25 Dec '12 - 14:43
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 - 25 Dec '12 - 14:44
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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 294
1 OriginalGriff 195
2 CPallini 163
3 Mahesh Bailwal 159
4 Tadit Dash 148
0 Sergey Alexandrovich Kryukov 10,169
1 OriginalGriff 7,749
2 CPallini 4,181
3 Rohan Leuva 3,482
4 Maciej Los 3,089


Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 25 Dec 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid