title: How to Serialized my class objects using kotlin?
body: <p>i have fragment and i need to pass my values Constructor to OncretaeView() methods so i receive my values in constructor and tr to convert my objects to serialized one but it does not convert. Here is my code. Please guide me how to Serialize and deserialize my objects .
  Thanks in advance</p>
  
  <pre><code>import java.io.Serializable
  
  class SendingFragment: Fragment(),Serializable {
  companion object {
          /**
           * new instance pattern for fragment
           */
          @JvmStatic
          fun newInstance(myObject: List&lt;TransactionEntity&gt;?, cc: Context, appDatabase: AppDatabase, networkDefinitionProvider: NetworkDefinitionProvider, incoming: TransactionAdapterDirection): SendingFragment {
  
              val gson = Gson()
              val gson1 = GsonBuilder().create()
              val model = myObject as List&lt;TransactionEntity&gt;
              val IT = gson.toJson(model)
              System.out.println("json representation :" + IT)
  
              val bo = ByteArrayOutputStream()
              val so = ObjectOutputStream(bo)
              so.writeObject(appDatabase)
              so.flush()
              val serializedObject = String(Base64.encode(bo.toByteArray()))
  
              val bundle = Bundle()
              bundle.putString("bundleValue", IT)
              bundle.putSerializable("serializedObject",serializedObject)
              val sendFragament: SendingFragment = SendingFragment()
              sendFragament.setArguments(bundle)
              return sendFragament
          }
      }
  
      override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
  
          val mFragserializedObject = arguments!!.getSerializable("serializedObject")
          System.out.println( "json serializedObject" + mFragserializedObject)
      }
  }
  </code></pre>
  
  <p><strong>Appdatabase.kt</strong></p>
  
  <pre><code>@Database(entities = {AddressBookEntry.class, Token.class, Balance.class, TransactionEntity.class}, version = 1)
  @TypeConverters({RoomTypeConverters.class})
  public abstract class AppDatabase extends RoomDatabase {
  
      public abstract AddressBookDAO getAddressBook();
  
      public abstract TokenDAO getTokens();
  
      public abstract TransactionDAO getTransactions();
  
      public abstract BalanceDAO getBalances();
  }
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <pre><code>class BeanDemo : Serializable {
  
      var MyAppDatabase: AppDatabase ? = null
  
      constructor() {
  
      }
  
      //secoutry constructor
  
      constructor(appDatabase: AppDatabase){
          this. MyAppDatabase =  appDatabase
          Log.d("appDatabase : Bean", "appDatabase$appDatabase")
      }
  
      //getter/setter methods
  
      fun getName(): AppDatabase? {
          Log.d("appDatabase : getName", "appDatabase$MyAppDatabase")
          return MyAppDatabase
      }
  
      fun setName(NEWAPPDB: AppDatabase) {
          Log.d("appDatabase : NEWAPPDB", "appDatabase$NEWAPPDB")
          MyAppDatabase = NEWAPPDB
      }
  }
  
  **myfragent.kt**
  
  
  
   fun newInstance(myObject: List&lt;TransactionEntity&gt;?, cc: Context, appDatabase: AppDatabase, networkDefinitionProvider: NetworkDefinitionProvider, incoming: TransactionAdapterDirection): SendingFragment {
  
                  val gson = Gson()
                  val gson1 = GsonBuilder().create()
                  val model = myObject as List&lt;TransactionEntity&gt;
                  val IT = gson.toJson(model)
      //             Here
                  val sampleVar = BeanDemo(appDatabase)
                  sampleVar.setName(appDatabase)
   val bundle = Bundle()
              bundle.putString("bundleValue", IT)
              bundle.putSerializable("serializedObject",sampleVar)
              val sendFragament: SendingFragment = SendingFragment()
              sendFragament.setArguments(bundle)
              return sendFragament
  
  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
  
  
  
          val bundle = arguments
          val obj = bundle!!.getSerializable("serializedObject") as BeanDemo
          val name = obj.getName()
  
          }
  </code></pre>
  
  <p>But <strong>val obj =</strong> is null, in setter method to hold the value but does not return it.</p>