Skip to Main Content

Serializing and desserializing an array

896976Feb 10 2014 — edited Feb 11 2014

Hey guys I need to this stuff (spend my life with pl\sql)

Anyway, I need to serialize an array, I have a class Account

I believe the serialize part is working I created a file    ("testArray.data");

But when I read the file and try to dump it in an array I got this null

I will appreciate any guidance on this

Before serialization

arrays_serialize2.Account@9931f5

arrays_serialize2.Account@19ee1ac

arrays_serialize2.Account@9931f5

arrays_serialize2.Account@19ee1ac

After serialization

null

BUILD SUCCESSFUL (total time: 0 seconds)

public class Arrays_serialize2 {

    private static Account[] accounts;

    private static Object Accounts;

    private static Iterable<Account> arr;

    private static Object newAccount2;

    private static Account[] laccounts;

    private static Account[] NewAccount;

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

      accounts = new Account[ 2 ]; // just 2 accounts for testing

      accounts[ 0 ] = new Account( 12345, 54321, 1000.0, 1200.0 );

      accounts[ 1 ] = new Account( 98765, 56789, 200.0, 200.0 );

 

          // int[] NewAccount = new int [2];

         

  int[] newAccount  = null; // will deserialize to this

       

         NewAccount  = new Account[ 2 ]; // will deserialize to this

                 //newTwoD = null; // will deserialize to this

      

        System.out.println("Before serialization");

  for (Account arr2 :  accounts) {

  for (Account val : accounts ) {

  System.out.println(val);

  }

  }

  try {

                  // writing array to disk

  FileOutputStream f_out = new FileOutputStream("testArray.data");

  ObjectOutputStream obj_out = new ObjectOutputStream(f_out);

  obj_out.writeObject(Accounts);

                 // reading array from disk

  FileInputStream f_in= new FileInputStream("testArray.data");

  ObjectInputStream obj_in = new ObjectInputStream(f_in);

  //newAccount = (int[]) iis.readObject();

                  NewAccount    = (Account[]) obj_in.readObject();

                             

                 System.out.println("After serialization");

             //  System.out.println(laccounts);

  for(int i=0; i<2;i++);

       System.out.println(NewAccount);

  } catch (IOException | ClassNotFoundException e) {

  }

    }

}

Here is the class Account

public class Account

{

   private int accountNumber; // account number

   private int pin; // PIN for authentication

   private double availableBalance; // funds available for withdrawal

   private double totalBalance; // funds available + pending deposits

   // Account constructor initializes attributes

   public Account( int theAccountNumber, int thePIN,

      double theAvailableBalance, double theTotalBalance )

   {

      accountNumber = theAccountNumber;

      pin = thePIN;

      availableBalance = theAvailableBalance;

      totalBalance = theTotalBalance;

   } // end Account constructor

   // determines whether a user-specified PIN matches PIN in Account

   public boolean validatePIN( int userPIN )

   {

      if ( userPIN == pin )

         return true;

      else

         return false;

   } // end method validatePIN

 

   // returns available balance

   public double getAvailableBalance()

   {

      return availableBalance;

   } // end getAvailableBalance

   // returns the total balance

   public double getTotalBalance()

   {

      return totalBalance;

   } // end method getTotalBalance

   // credits an amount to the account

   public void credit( double amount )

   {

      totalBalance += amount; // add to total balance

   } // end method credit

   // debits an amount from the account

   public void debit( double amount )

   {

      availableBalance -= amount; // subtract from available balance

      totalBalance -= amount; // subtract from total balance

   } // end method debit

   // returns account number

   public int getAccountNumber()

   {

      return accountNumber;

   } // end method getAccountNumber

} // end class Account

Comments
Post Details
Added on Feb 10 2014
2 comments
1,314 views