0

I'm trying to serialize a 3D jagged array of a class I've created, and I'm getting the following error from within BinaryFormatter when I call binaryFormatter.Serialize():

NullReferenceException: Object reference not set to an instance of an object System.Runtime.Serialization.Formatters.Binary.ObjectWriter.GetAssemblyNameId (System.String assembly) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:812) System.Runtime.Serialization.Formatters.Binary.ObjectWriter.GetAssemblyId (System.Reflection.Assembly assembly) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:807)...

The most obvious potential issue, would be that the jagged array isn't correctly and fully initialized, but that isn't the case. I've tested multiple initialization strategies, and, if it wasn't properly initialized, the application would have had problems way before serialization.

It's important to note, after the jagged array is initialized, I loop through the array and instantiate ExampleClass() for each index.

I'm doing this in Unity 3D.

What am I missing?

    BinaryFormatter binaryFormatter = new BinaryFormatter();

    FileStream fileStream = File.Create(Application.persistentDataPath + "/data.dat");

    // Initialize the jagged array      
    ExampleClass[][][] exampleClassArray = new ExampleClass[32][][];

    for (int i = 0; i < exampleClassArray.Length; i++)
    {
        exampleClassArray[i] = new ExampleClass[32][];

        for (int j = 0; j < exampleClassArray[i].Length; j++)
        {
            exampleClassArray[i][j] = new ExampleClass[32];
        }
    }

    // Populate the jagged array
    for (int i = 0; i < 32; i++)
    {
        for (int j = 0; j < 32; j++)
        {
            for (int k = 0; k < 32; k++)
            {
                exampleClassArray[i][j][k] = new ExampleClass();
            }
        }
    }

    binaryFormatter.Serialize(fileStream, exampleClassArray);

    fileStream.Close();
dbc
  • 104,963
  • 20
  • 228
  • 340
Daniel Carvalho
  • 557
  • 2
  • 8
  • 20
  • 1. Learn [How to use a debugger](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). 2. Read [What is a NullReferenceException?](https://stackoverflow.com/questions/4660142/). 3. Tell us which line throws the NullReferenceException. 4. You've initialized an empty array of `new ExampleClass[32]` but never actually populated it with any `ExampleClass`es. – Dour High Arch Jun 17 '17 at 20:22
  • 1
    Updated the question to be more explicit, so I don't get comments like yours. – Daniel Carvalho Jun 17 '17 at 20:28
  • 1
    I can't reproduce this on Microsoft .Net, see https://dotnetfiddle.net/GcpjUO to see your code working there. Since this seems to be unity3d-specific you might want to report an issue with them. Does this happen with a 2d jagged array? A 1d simple array? – dbc Jun 17 '17 at 23:45
  • @dbc You're probably right about directing this to Unity folks. A 1D simple array works fine, 2D and upwards breaks. So sad that this seems to regular C# and not Unity! – Daniel Carvalho Jun 18 '17 at 00:28
  • Probably to do with Mono and the .NET version supported by Unity. I'm guessing. – Daniel Carvalho Jun 18 '17 at 00:31
  • @DanielCarvalho - To my recollection, as far back as .Net 2.0 (2007) `BinaryFormatter` was able to serialize jagged arrays. So this looks to be a bug in their (re)implementation of `BinaryFormatter`. – dbc Jun 18 '17 at 03:41
  • There was a time when Unity could not serialize jagged array. Can't find any recent thread about it but I guess this is what you are facing there. Solution would be to turn your jagged array into 2D array. If the result is using too much empty spaces, you could use BinaryWriter and a int telling the size of each level and then all objects of the corresponding array. Bit more code, but smaller. – Everts Jun 18 '17 at 13:22

0 Answers0