窗口不会出现Java和堆栈溢出错误

时间:2016-04-10 03:36:50

标签: java

我在使用Java程序显示窗口时遇到问题,运行程序时出现堆栈溢出错误。

以下是我遇到问题的代码示例:

public Ballot() {
     bp.addBallot();
     frame.setTitle("Ballot");
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setSize(430,350);
     frame.setLocation(300,100);
     frame.setResizable(false);
     Container cont = frame.getContentPane();
     cont.setLayout(null);
     cont.setBackground(Color.GRAY);
     ButtonGroup bg = new ButtonGroup();
     JRadioButton[] buttons = new JRadioButton[numButtons];
     for (int i = 0; i < buttons.length; i++) {
         bg.add(buttons[i]);
     }
     JLabel label = new JLabel("Make your choice");
     label.setForeground(Color.BLUE);
     label.setFont(new Font("Arial", Font.BOLD, 11));
     next = new JButton("Next");
     next.setForeground(Color.GREEN);
     next.addActionListener(this);
     panel = new JPanel();
     for (int i = 0; i < buttons.length; i++) {
         panel.add(buttons[i]);
     }
     panel.add(next);
     cont.add(panel);
     setVisible(true);
 }

我想知道我的代码出了什么问题。

可能有用的其他代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.io.*;

public class Ballot extends JPanel implements ActionListener {
  JPanel panel;
  JFrame frame = new JFrame();
  int numButtons;
  JRadioButton[] buttons;
  JButton next = new JButton();
  int votedOrNot;
  BallotProgram bp = new BallotProgram();

  public Ballot() {
     bp.addBallot();
     frame.setTitle("Ballot");
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.setSize(430,350);
     frame.setLocation(300,100);
     frame.setResizable(false);
     frame.pack();
     frame.setVisible(true);
     Container cont = frame.getContentPane();
     cont.setLayout(null);
     cont.setBackground(Color.GRAY);
     ButtonGroup bg = new ButtonGroup();
     JRadioButton[] buttons = new JRadioButton[numButtons];
     for (int i = 0; i < buttons.length; i++) {
         bg.add(buttons[i]);
     }
     JLabel label = new JLabel("Make your choice");
     label.setForeground(Color.BLUE);
     label.setFont(new Font("Arial", Font.BOLD, 11));
     next = new JButton("Next");
     next.setForeground(Color.GREEN);
     next.addActionListener(this);
     panel = new JPanel();
     for (int i = 0; i < buttons.length; i++) {
         panel.add(buttons[i]);
     }
     panel.add(next);
     cont.add(panel);
     setVisible(true);
 }

 public void actionPerformed(ActionEvent a) {
     if (next.getText().equals("Cast Vote")) {
        if (votedOrNot == 0) {
            votedOrNot++;
        }
        else {
        }
     }
     else if (next.getText().equals("Login to Vote")) {
     }
  }

}

另一堂课:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.io.*;

public class BallotProgram {

 String[] choices;
 int numOfChoices;

 public ArrayList<Ballot> addBallot() {
    File file = new File("ballots.txt");
    boolean exists = file.exists();
    ArrayList<Ballot> ballot = new ArrayList<Ballot>();
    Ballot b = new Ballot();
    try {
       Scanner s = new Scanner(file);
       while (s.hasNextLine()) {
           int ballotID = s.nextInt();
           s.nextLine();
           String ballotTitle = s.nextLine();
           choices = new String[numOfChoices];
           b.buttons = new JRadioButton[b.numButtons];
           for (int i = 0; i < b.numButtons; i++) {
                choices[i] = s.nextLine();
                b.buttons[i] = new JRadioButton(choices[i]);
           }
           Ballot bt = new Ballot();
           ballot.add(bt);
       }
       s.close();
    } catch (IOException f) {
      System.out.println("can't find the file");
    }
    return ballot;
 }

我的终端说:

Exception in thread "main" Exception in thread "main" java.lang.StackOverflowError
at sun.awt.X11GraphicsConfig.pGetBounds(Native Method)
at sun.awt.X11GraphicsConfig.getBounds(X11GraphicsConfig.java:314)
at java.awt.Window.init(Window.java:505)
at java.awt.Window.<init>(Window.java:537)
at java.awt.Frame.<init>(Frame.java:420)
at java.awt.Frame.<init>(Frame.java:385)
at javax.swing.JFrame.<init>(JFrame.java:189)
at Ballot.<init>(Ballot.java:10)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)

由于

1 个答案:

答案 0 :(得分:0)

StackOverflowError发生在我们最好的人身上。地狱,网站以它命名。这是每个有抱负的程序员遇到的事情。大多数情况下(对于较新的程序员),当一个方法调用调用调用它的方法的方法时,会导致StackOverflowError。基本上,会创建一个无限循环的方法调用。

根据堆栈跟踪信息...

at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)
at BallotProgram.addBallot(BallotProgram.java:17)
at Ballot.<init>(Ballot.java:18)

我们发现程序中的两点之间存在明显的循环。当我们调查这些要点时......

public ArrayList<Ballot> addBallot() {
    ...
    Ballot b = new Ballot();
    ...
}

public Ballot() {
    bp.addBallot();
    ...
}

我们看到在这里创建了无限循环。当我们创建一个新的Ballot对象时,它的构造函数会调用bp.addBallot(),然后命中Ballot b = new Ballot()行,返回构造函数。在创建新Ballot对象的过程中,会反复创建一个新的Ballot对象,直到StackOverflowError被抛出。