title: Wondering how to read in objects from .dat file
body: <p>so im basically trying to make a "Bank" program.</p>
  
  <p>So far ( i think) i have gotten a way to write objects to a .dat file for future use, and i was wanting to read in these objects to an ArrayList every time it starts so you can access previously made accounts.</p>
  
  <p>I keep getting the "java.lang.IndexOutOfBoundsException"  at mainBank.main(mainBank.java:22)</p>
  
  <p>here is the code for mainBank:</p>
  
  <pre><code>    import java.util.*;
      import java.io.*;
  
      public class mainBank 
     {
  private static ArrayList&lt;Account&gt; userList = new ArrayList&lt;Account&gt;();
  private static int userNum;
  private static Scanner scan = new Scanner(System.in);
  public static void main(String args[]) throws IOException
  {
      try
      {
          readFromArray();
      }
      catch(Exception e)
      {
          e.getStackTrace();
      }
      System.out.println("Welcome to the bank");
      System.out.println("Account ID");
          int uID = scan.nextInt();
      Account currAccount = new Account((Account)userList.get(uID));
  
      nextAction(currAccount);
  
  }
  
  public static void nextAction(Account acc)
  {   System.out.println(acc);
  
      System.out.print("Are you happy with this y/n? ");
      String input = scan.nextLine();
  if(input.toLowerCase().equals("y"))
  {
      System.out.println("Thank you Come Again");
  }
  else
  {
      System.out.println("What would you like to do?");
      System.out.println("1.Deposit");
      System.out.println("2.Widthdraw");
      System.out.println("3.Exit");
          int choice = scan.nextInt();
      switch(choice)
      {
      case 1: System.out.print("Deposit Ammount: ");
              acc.deposit(scan.nextInt());
              nextAction(acc);
  
              break;
  
      case 2: System.out.print("Widthdrawl Ammount: ");
              try{acc.withdraw(scan.nextInt());}
              catch(Exception e){System.out.println("not enough money");}
              nextAction(acc);
  
              break;
  
      case 3: System.out.print("Okay, Good Bye!");
              break;
      }
  }
  
  
  }
  public static void readFromArray() throws IOException, Exception
  {
      FileInputStream fis = new FileInputStream("data.dat");
      ObjectInputStream ois = new ObjectInputStream(fis);
      Account acc = new Account((Account) ois.readObject());
  
  
      for(int i = 0; i&lt;userNum;i++)
      {       
          acc = (Account) ois.readObject();
          userList.add(acc);
      }
  }
  public static void writeToFile() throws IOException
  {
      FileOutputStream fos = new FileOutputStream("data.dat");
      ObjectOutputStream oos = new ObjectOutputStream(fos);
  
  
      for(int i =0;i&lt;userList.size();i++)
      {
          Account acc = userList.get(i);
          oos.writeObject(acc);
      }
      oos.flush();
      oos.close();
  }
      }
  </code></pre>
  
  <p>Account class:</p>
  
  <pre><code>    import java.io.Serializable;
  
      public class Account implements Serializable
      {
  private int MONEY;
  private String NAME;
  private String PASSWORD;
  
  public Account(Account acc)
  {
      MONEY = acc.getMoney();
      NAME = acc.getName();
      PASSWORD = acc.getPassword();
  }
  private String getPassword() 
  {
      return PASSWORD;
  }
  public Account(String n,String p,int m)
  {
  
      MONEY = m;
      NAME =n;
      PASSWORD = p;
  }
  
  public void setMoney(int m)
  {
      MONEY = m;
  }
  public void setName(String n)
  {
      NAME=n;
  }
  public void deposit(int m)
  {
      MONEY +=m;
  }
  public void withdraw(int m) throws NotEnoughMoneyException
  {
      if(MONEY-m&lt;0)
          throw new NotEnoughMoneyException();
      else
          MONEY -=m;
  }
  
  public int getMoney()
  {
      return MONEY;
  
  }
  public String getName()
  {
      return NAME;
  
  }
  
  public String toString()
  {
      String s = getName()+":"+getMoney();
      return s;
  }
  
      }
  
       class NotEnoughMoneyException extends Exception
     {
  String msg;
  
  NotEnoughMoneyException()
  {
      msg = "Not enough Money";
  }
      }
  </code></pre>