Reputation: 3
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);
Upvotes: 0
Views: 1967
Reputation: 4549
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.
Upvotes: 8