- Home /
When attempting to serialize and save a jagged array it gives me an error message
Problem: I have a script which creates an array, then it creates a jagged array, then a 3d jagged array (I think I got the names correct) Each array starting with the smallest is in the next. The "foreach" part is something I have been trying to do (Print array to console):
 public void SaveWeights ()
     {
 
         //Creates a binaray formatter
         BinaryFormatter binaryFormatter = new BinaryFormatter();
         
         //FileStream saveFile = File.Create("C:/Users/user/Desktop" + neuronWeights[k] + ".sav");
         
         //Creates a filepath
         FileStream saveFile = File.Open("C:/Users/user/Desktop" + "/weightData.txt", FileMode.Open);
         
         //Creates a container for data
         WeightData weightData = new WeightData();
         weightData.saveWeights = saveWeights; //Sets the clean serializable data to local data
         
         //Writes container data to filepath
         binaryFormatter.Serialize(saveFile, saveWeights);
         saveFile.Close();
 
         //Debug.Log(saveWeights);
 
         foreach (float[][] i in saveWeights)
         {
             Debug.Log(i);
         }
 
     }
 
 }
 
 ///Clean data which is serializable
 [Serializable]
 class WeightData
 {
     public float[][][] saveWeights;
 }
When I try to serialize then save the largest 3d jagged array it gives me this error:
 ArgumentNullException: Argument cannot be null.
 Parameter name: obj
 System.Runtime.Serialization.ObjectIDGenerator.GetId (System.Object obj, System.Boolean& firstTime) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization/ObjectIDGenerator.cs:82)
 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectInstance (System.IO.BinaryWriter writer, System.Object obj, Boolean isValueObject) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:284)
 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteQueuedObjects (System.IO.BinaryWriter writer) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:271)
 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectGraph (System.IO.BinaryWriter writer, System.Object obj, System.Runtime.Remoting.Messaging.Header[] headers) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:256)
 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph, System.Runtime.Remoting.Messaging.Header[] headers) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:232)
 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:211)
 NeuralNetwork.SaveWeights () (at Assets/Scripts/NeuralNetwork/NeuralNetwork.cs:229)
 UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:154)
 UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:637)
 UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:773)
 UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:52)
 UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36)
 UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45)
 UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
 UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
 UnityEngine.EventSystems.EventSystem:Update()
Then right after that error it gives me this one:
 IOException: Sharing violation on path C:\Users\user\Desktop\weightData.txt
 System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/FileStream.cs:320)
 System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share)
 (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
 System.IO.File.Open (System.String path, FileMode mode) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/File.cs:347)
 NeuralNetwork.SaveWeights () (at Assets/Scripts/NeuralNetwork/NeuralNetwork.cs:222)
 UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:154)
 UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:637)
 UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:773)
 UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:52)
 UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36)
 UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45)
 UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
 UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
 UnityEngine.EventSystems.EventSystem:Update()
Question: Why is it giving me these errors? How can I fix it? Do I need to rewrite the structuring because of some problem I have not seen yet?
Notes: I am calling the SaveWeights function from a canvas ui object. I am using the MonoBehaviour class and the IComparable interface so:
 public class NeuralNetwork : MonoBehaviour, IComparable<NeuralNetwork> {}
I do know that the "new" operator is not usable in MonoBehaviour class but it seems to work fine. It does however give me an error, but like I said it seems to work fine:
 You are trying to create a MonoBehaviour using the 'new' keyword.  This is not allowed.  MonoBehaviours can only be added using AddComponent().  Alternatively, your script can inherit from ScriptableObject or no base class at all
 UnityEngine.MonoBehaviour:.ctor()
 NeuralNetwork:.ctor(Int32[]) (at Assets/Scripts/NeuralNetwork/NeuralNetwork.cs:18)
 NeuralManager:InitBikeNeuralNetworks() (at Assets/Scripts/NeuralNetwork/NeuralManager.cs:153)
 NeuralManager:Update() (at Assets/Scripts/NeuralNetwork/NeuralManager.cs:32)
I will post any scripts if anyone asks to see my code to find errors.
Answer by manuelcque · May 20, 2020 at 07:15 AM
Hello, I read on your post 2 or 3 days ago for the same problem. "Nice ! One person who does the neural network on unity" ... whatever the architecture of the programs seem to differ.
I followed this tutorial (in French) https://www.youtube.com/watch?v=21gN4YPQZa0&feature=youtu.be&t=44
And resumed the code linked to this tutorial: https://drive.google.com/file/d/11Z10vmuIxVCKBT7gAlbFQ2RlPuSgnKyI/view
I have seen similar errors circulating at times.
 [Serializable]
  class WeightData
  {
      public float[][][] saveWeights;
  }
I had to add a constructor so that Unity is happy...
 public class NeuralNetwork : IComparable<NeuralNetwork>
 {
 
 
     public object SendWeightsToSave()
     {
         SerializableNeuralNetwork snn = new SerializableNeuralNetwork(weights, fitness);
         return snn;
     }
 
     public string ReceiveWeightsFromLoad()
     {
         return "";
     }
 
 }
 
 [System.Serializable]
 public class SerializableNeuralNetwork
 {
     public float[][][] weights;//matrice des poids
     public float fitness;       //fitness du reseau
 
     public SerializableNeuralNetwork(float[][][] patternWeights, float patternFitness)
     {
         weights = patternWeights;
         fitness = patternFitness;
     }
 }
After doing this, I opted for a json backup and i use JSON NET. It seemed simpler ...
(sorry for my approximate english)
Your answer
 
 
             Follow this Question
Related Questions
Serialization Error 1 Answer
Save and load serialization problem 0 Answers
Cannot save(serialize) a double array of a custom class 1 Answer
How do I save a custom class of variables to playerprefs? 2 Answers
Array not updating in inspector 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                