title: ArrayList implemented in custom list class Java
body: <p>I am trying to write a class that implements arraylist, but I will observe and repaint my gui whenever an element is added to the list, or removed from the list. Everything is syntactically right, but there seems to be a logic error in my code because I am getting a stackoverflowerror which leads me to believe that there is a recursive call somewhere that I am missing. The exact error is 
  <code>Exception in thread "main" java.lang.StackOverflowError
      at java.util.AbstractList.&lt;init&gt;(Unknown Source)
      at java.util.ArrayList.&lt;init&gt;(Unknown Source)
      at java.util.ArrayList.&lt;init&gt;(Unknown Source)
      at Library.&lt;init&gt;(Library.java:45)
      at LoanList.&lt;init&gt;(LoanList.java:14)
      at Library.&lt;init&gt;(Library.java:47)
      at LoanList.&lt;init&gt;(LoanList.java:14)</code></p>
  
  <p>the actual code is around around 1k(1,000) lines so I will just post the bits that I think are to blame. </p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.GridLayout;
  import java.awt.Toolkit;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.io.Serializable;
  import java.text.DateFormat;
  import java.text.SimpleDateFormat;
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Comparator;
  import java.util.Date;
  import java.util.Iterator;
  import java.util.List;
  import javax.swing.BoxLayout;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JOptionPane;
  import javax.swing.JPanel;
  import javax.swing.JScrollBar;
  import javax.swing.JScrollPane;
  import javax.swing.JTextArea;
  import javax.swing.JTextField;
  import javax.swing.event.ChangeEvent;
  import javax.swing.event.ChangeListener;
  public class Library implements Serializable, LibraryInterface, ActionListener{
      private static final long serialVersionUID = 1L;
      private List&lt;Student&gt; students;
      private List&lt;Book&gt; books;
      private LoanList&lt;Loan&gt; loans;
      private JButton ok, cancel;
      private JTextField utxt,ntxt,etxt;
      private JTextArea list;
      private JFrame frame;
      public Library(){
          students = new ArrayList&lt;Student&gt;();
          books = new ArrayList&lt;Book&gt;();
          loans = new LoanList&lt;Loan&gt;();
      }
      /* (non-Javadoc)
       * @see LibraryInterface#loadBooks()
       */
      /* (non-Javadoc)
       * @see LibraryInterface#loadBooks()
       */
      @Override
      public void loadBooks() throws ClassNotFoundException, IOException{
          FileInputStream fis = new FileInputStream("Books.txt");
          ObjectInputStream ois = new ObjectInputStream(fis);
          books = (ArrayList&lt;Book&gt;) ois.readObject();
          ois.close();
      }
      /* (non-Javadoc)
       * @see LibraryInterface#loadStudents()
       */
      /* (non-Javadoc)
       * @see LibraryInterface#loadStudents()
       */
      @Override
      public void loadStudents() throws ClassNotFoundException, IOException{
          FileInputStream fis = new FileInputStream("Students.txt");
          ObjectInputStream ois = new ObjectInputStream(fis);
          students = (ArrayList&lt;Student&gt;) ois.readObject();
          ois.close();
      }
      /* (non-Javadoc)
       * @see LibraryInterface#loadLoans()
       */
      /* (non-Javadoc)
       * @see LibraryInterface#loadLoans()
       */
  
      @Override
      public void loadLoans() throws ClassNotFoundException, IOException{
          FileInputStream fis = new FileInputStream("Loans.txt");
          ObjectInputStream ois = new ObjectInputStream(fis);
          loans = (LoanList&lt;Loan&gt;) ois.readObject();
          ois.close();
      }
  
  
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.Iterator;
  import java.util.List;
  import java.util.ListIterator;
  
  public class LoanList&lt;Loan&gt; extends Library implements List&lt;Loan&gt;{
       /**
       * 
       */
      private static final long serialVersionUID = 1L;
      private ArrayList&lt;Loan&gt; list;
  
       public LoanList(){
           list = new ArrayList&lt;Loan&gt;();
       }//Rest of the arraylist methods follow, however it's not constructing so no need to post
  
  import java.io.Serializable;
  
  public class Loan implements Serializable{
      private String uid;
      private String bookId;
      private String date;
  
      public Loan(String uid,String bookid,String date){
          this.uid = uid;
          this.bookId = bookid;
          this.date = date;
      }
      public String getUid() {
          return uid;
      }
      public String getBookId() {
          return bookId;
      }
      public String getDate() {
          return date;
      }
      public String toString(){
          String x="";
          x = "BookID: " + this.bookId + "\n" + "Student UID: " + this.uid + "\n" + "Date Checkedout: " + this.date + "\n" + "\n";
          return x;
      }
  </code></pre>
  
  <p><strong>EDIT</strong> new repaint method</p>
  
  <pre><code>public void framePaint(){
      Collections.sort(students,new studentComparator());
      String result="";
      for(int i=0;i&lt;students.size();i++){
          result += "\n" + students.get(i).toString() + "\n";
          for(int j=0;j&lt;loans.size();j++){
              if(loans.get(j).getUid().equals(students.get(i).getUid())){
                  result += "Book ID: " + loans.get(j).getBookId() + "\n";
              }
          }
      }
      list.setText(result);
      frame.repaint();
  </code></pre>
  
  <p><strong>Comparator for reference</strong></p>
  
  <pre><code>    public class studentComparator implements Comparator&lt;Student&gt;{
      @Override
      public int compare(Student arg0, Student arg1) {
          return arg0.getUid().compareTo(arg1.getUid());
      }
  }
  </code></pre>
  
  <p><strong>GUI for frame i am trying to repaint</strong></p>
  
  <pre><code>    public void listStudents(){
      frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      JPanel panel = new JPanel();
      panel.setLayout(new BoxLayout(panel,1));
      list = new JTextArea("haha");
      list.setEditable(false);
      list.setBackground(Color.YELLOW);
      list.setForeground(Color.BLACK);
      Collections.sort(students,new studentComparator());
      String result="";
      for(int i=0;i&lt;students.size();i++){
          result += "\n" + students.get(i).toString() + "\n";
          for(int j=0;j&lt;loans.size();j++){
              if(loans.get(j).getUid().equals(students.get(i).getUid())){
                  result += "Book ID: " + loans.get(j).getBookId() + "\n";
              }
          }
      }
      list.setText(result);
      panel.add(list);
      JScrollPane scroll = new JScrollPane(panel);
      frame.add(scroll);
      frame.setResizable(true);
      frame.setSize(300, 300);
      Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
      frame.setLocation(dim.width/2-frame.getSize().width/2, dim.height/2-frame.getSize().height/2);
      frame.setVisible(true);
  </code></pre>