How to fix an inner NullReferenceException when working with a serializable class using the ISerializable interface?

1

I'm working on a project which uses 5 different libraries as modules. My task is to save and load this information from one of these libraries using another. I've made the functionality to save, however the loading/deserializing of the data gives me an Inner NullReferenceException. Here is the stack trace:

System.Reflection.TargetInvocationException
  HResult=0x80131604
  Message=Exception has been thrown by the target of an invocation.
  Source=mscorlib
  StackTrace:
   at System.RuntimeMethodHandle.SerializationInvoke(IRuntimeMethodInfo method, Object target, SerializationInfo info, StreamingContext& context)
   at System.Runtime.Serialization.ObjectManager.CompleteISerializableObject(Object obj, SerializationInfo info, StreamingContext context) in f:\dd\ndp\clr\src\BCL\system\runtime\serialization\objectmanager.cs:line 884
   at System.Runtime.Serialization.ObjectManager.FixupSpecialObject(ObjectHolder holder) in f:\dd\ndp\clr\src\BCL\system\runtime\serialization\objectmanager.cs:line 283
   at System.Runtime.Serialization.ObjectManager.DoFixups() in f:\dd\ndp\clr\src\BCL\system\runtime\serialization\objectmanager.cs:line 935
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) in f:\dd\ndp\clr\src\BCL\system\runtime\serialization\formatters\binary\binaryobjectreader.cs:line 179
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) in f:\dd\ndp\clr\src\BCL\system\runtime\serialization\formatters\binary\binaryformatter.cs:line 197
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream) in f:\dd\ndp\clr\src\BCL\system\runtime\serialization\formatters\binary\binaryformatter.cs:line 111
   at WarehouseDistribution.SimulationForm.btnLoad_Click(Object sender, EventArgs e) in C:\Users\anast\Desktop\Final Product\WarehouseDistribution\SimulationForm.cs:line 650
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at WarehouseDistribution.Program.Main() in C:\Users\anast\Desktop\Final Product\WarehouseDistribution\Program.cs:line 19

Inner Exception 1:
NullReferenceException: Object reference not set to an instance of an object.

I've looked through a lot of other threads that might help me and have tried things such as What could be causing a System.TypeLoadException? and BinaryFormatter.Deserialize "unable to find assembly" after ILMerge but nothing has worked.

Here is the gist of the method that deserializes the given file:

    IFormatter formatter = new BinaryFormatter();
    fs = new FileStream(openFileDialog1.FileName, FileMode.Open);
    importExporter = (ImportExporter)formatter.Deserialize(fs); //loads the saved ImportExporter

    panel1.BackgroundImage = importExporter.GetMap(); //initial test

Here is the main part of the ISerializable class that I want to use for loading/saving:

    public ImportExporter(RoadManager rm, Bitmap map, Profile p)
    {
        savedRoadmanager = rm;
        savedMap = map;
        savedProfile = p;
    }

    //ISerializable constructor that loads all the needed information
    public ImportExporter(SerializationInfo info, StreamingContext context)
    {
        this.savedRoadmanager = (RoadManager)info.GetValue("savedRoadmanager", savedRoadmanager.GetType());
        this.savedMap = (Bitmap)info.GetValue("savedMap", savedMap.GetType());
        this.savedProfile = (Profile)info.GetValue("savedProfile", savedProfile.GetType());
    }


    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("savedRoadmanager", savedRoadmanager, savedRoadmanager.GetType());
        info.AddValue("savedMap", savedMap, savedMap.GetType());
        info.AddValue("savedProfile", savedProfile, savedProfile.GetType());
    }

I did some testing to get familiar with ISerializable and had no such problems. I feel like the source might be related to that interface, or it might be something simpler. Ideally, I would want this to give no error so I can use the given data to display information on the form. I've been working on this for so many hours now that everything is getting jumbled up in my head so it's been difficult to debug, on top of it being new material for me. Any assistance would be greatly appreciated.

c#
serialization
binary
formatter
iserializable

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0