1

I am Serializing a class object and storing in sqlite database as BLOB from a C# application.

Function used for serializing: public static byte[] ObjectToByteArray (AnalyzeSubstring obj)

And on other application, fetching the blob database and converting blob database in to the object by deserializing. function for deserializing is :public static AnalyzeSubstring ByteArrayToObject (byte[] arrBytes)

public static byte[] ObjectToByteArray (AnalyzeSubstring obj)
              {
              if ( obj == null )
                  return null;
              BinaryFormatter bf = new BinaryFormatter ();
              //bf.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
              MemoryStream ms = new MemoryStream ();
              bf.Serialize (ms, obj);
              return ms.ToArray ();
              }

          public static AnalyzeSubstring ByteArrayToObject (byte[] arrBytes)
              {
              MemoryStream memStream = new MemoryStream ();
              BinaryFormatter binForm = new BinaryFormatter ();
              memStream.Write (arrBytes, 0, arrBytes.Length);
              memStream.Seek (0, SeekOrigin.Begin);
              AnalyzeSubstring obj = (AnalyzeSubstring) binForm.Deserialize (memStream);
              return obj;
              }

Issue:In line while deserializing AnalyzeSubstring obj = (AnalyzeSubstring) binForm.Deserialize (memStream);.

It fails with error

Object reference not set to an instance of an object .

However if we do both operation in same application only, then this work properly.

Please help

  • how have you declared AnalyzeSubstring. It suggests that this has not been instanciated correctly when seperating the Method. Dont forget you will have to reference it through the project dependencies. – bilpor Dec 19 '16 at 11:32
  • I have used the same class AnalyzeSubstring in both applications.how to reference it through project dependencies? – user4080605 Dec 19 '16 at 11:36
  • in VS you create a solution. You then create 1:N Projects in that solution. your First project will have the method in a Class. The second project then needs to make a reference to the first project to gain access to it's properties and methods. Right mouse click on the second projects references and select add reference. – bilpor Dec 19 '16 at 11:59
  • ok.. lemme try that..:) – user4080605 Dec 19 '16 at 12:06
  • @bilpor: Is there any way, in which I dont require to add reference. and make two projects independent . From my application I simply want to fetch the blob data from database. – user4080605 Dec 19 '16 at 12:08
  • you'd have to write the method twice. Once in each project, but the way to go is to perhaps create a 'Common' dll with the methods in. You create this in a solution. When you compile it it becomes a single Dll in its own right. Then in your other projects you reference the dll in the same way. You point to the compiled dll. – bilpor Dec 19 '16 at 12:42
  • You need to either extract `AnalyzeSubstring` to a shared DLL or create a `SerializationBinder` that maps between them. See [Deserialization with binaryformatter](http://stackoverflow.com/a/30198132/3744182) for three possible options. See also [How to get BinaryFormatter to deserialize in a different application](http://stackoverflow.com/a/7534790/3744182). – dbc Dec 19 '16 at 18:15

0 Answers0