0

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 Exception in thread "main" java.lang.StackOverflowError at java.util.AbstractList.<init>(Unknown Source) at java.util.ArrayList.<init>(Unknown Source) at java.util.ArrayList.<init>(Unknown Source) at Library.<init>(Library.java:45) at LoanList.<init>(LoanList.java:14) at Library.<init>(Library.java:47) at LoanList.<init>(LoanList.java:14)

the actual code is around around 1k(1,000) lines so I will just post the bits that I think are to blame.

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<Student> students;
    private List<Book> books;
    private LoanList<Loan> loans;
    private JButton ok, cancel;
    private JTextField utxt,ntxt,etxt;
    private JTextArea list;
    private JFrame frame;
    public Library(){
        students = new ArrayList<Student>();
        books = new ArrayList<Book>();
        loans = new LoanList<Loan>();
    }
    /* (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<Book>) 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<Student>) 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<Loan>) 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<Loan> extends Library implements List<Loan>{
     /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private ArrayList<Loan> list;

     public LoanList(){
         list = new ArrayList<Loan>();
     }//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;
    }

EDIT new repaint method

public void framePaint(){
    Collections.sort(students,new studentComparator());
    String result="";
    for(int i=0;i<students.size();i++){
        result += "\n" + students.get(i).toString() + "\n";
        for(int j=0;j<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();

Comparator for reference

    public class studentComparator implements Comparator<Student>{
    @Override
    public int compare(Student arg0, Student arg1) {
        return arg0.getUid().compareTo(arg1.getUid());
    }
}

GUI for frame i am trying to repaint

    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<students.size();i++){
        result += "\n" + students.get(i).toString() + "\n";
        for(int j=0;j<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);
1
  • I think you can remove class Loan and all the imports. Commented Feb 28, 2014 at 0:25

1 Answer 1

8

LoanList is extending Library. Library's constructor makes a new LoanList. This new LoanList is a Library, so it creates a new LoanList. This new LoanList is a Library, so it creates a new LoanList. This new LoanList is a Library, so it creates a new LoanList. ad infinitum.

Sign up to request clarification or add additional context in comments.

7 Comments

To put it another way, a Library has a LoanList. A LoanList is a Library so it has a LoanList which is a Library so it has a LoanList which is a Library so it has a LoanList which is a Library so it has a LoanLi... LoanList probably doesn't need to be a Library.
So what would the correction be? I need to extend library in order to for it to access a method that repaints my JFrame named frame. The point of implementing my own ArrayList was to create an observer that whenever an object was added or removed from the list, I would invoke the repaint method in Library that would repaint my frame with an updated list. Inside my framePaint method was frame.repaint()
You have two choices. Make LoanList not an Library (remove the "extends Library"), or make Libary not have a LoanList.
Why can't LoanList just be passed a reference to the Library that needs to be repainted? Is a LoanList a Library? That's what you're saying when you write LoanList extends Library, that a LoanList is a kind of Library.
I agree with @DavidConrad, You want to pass Library into LoanList.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.