使用类

时间:2017-12-29 00:35:29

标签: java stack-overflow

所以我正在制作工资单应用程序并遇到了问题。每次我去运行它,它都会出现stackoverflow错误。我似乎无法找出错误。我一直坚持这个问题一段时间,但任何帮助将不胜感激。我在分配奖金时注释掉了mainform变量,它似乎有效但我需要该变量才能回到其他形式。

Exception in thread "main" java.lang.StackOverflowError
    at sun.awt.CGraphicsConfig.nativeGetBounds(Native Method)
    at sun.awt.CGraphicsConfig.getBounds(CGraphicsConfig.java:56)
    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 oca.project.MainForm.<init>(MainForm.java:91)
    at oca.project.AssignBonusForm.<init>(AssignBonusForm.java:80)
    at oca.project.MainForm.<init>(MainForm.java:22)

主要班级

package oca.project;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class OCAProject {

 static ArrayList<Person> persons = new ArrayList<>();

    public static void main(String[] args) {

         CEO JamesMint = new CEO();
        //making objects 
 AdministrationManager BillJohns = new AdministrationManager(JamesMint);
FinancialAdministrator EricWhite = new FinancialAdministrator(JamesMint);
 persons.add(JamesMint);
 persons.add(BillJohns);
persons.add(EricWhite);

 //declaring form and passing arraylist as parameter
        MainForm frame = new MainForm(persons);
        frame.setVisible(true); 
    }
}

大型机类

package oca.project;
import java.util.ArrayList;

public class MainForm extends javax.swing.JFrame {
  //array list to hold the report items
    private ArrayList<PayrollReportItem> payRolllist = new ArrayList<>();

  AssignBonusForm assignFrame = new AssignBonusForm(payRolllist);
void setList(ArrayList<Person> persons) {
//sets arraylist

      assignFrame.setPersons(persons);


   }
 public MainForm(ArrayList<Person> persons) {
           initComponents();
        setList(persons);
       }

 private void btnAssignBonusActionPerformed(java.awt.event.ActionEvent evt) {                                               

//to open assign bonus form
      assignFrame.setVisible(true);
        this.dispose();


    }   

分配奖金表格

package oca.project;

import java.util.ArrayList;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;


/**
 *
 * Form that allows managers assign bonuses to their subordinates 
 */
public class AssignBonusForm extends javax.swing.JFrame  {

 static ArrayList<Person> persons = new ArrayList<>();

  /**
     * Creates new form AssignBonusForm
     * @param payRolllist
     */


    public AssignBonusForm(ArrayList<PayrollReportItem> payRolllist) {
        initComponents();


      System.out.println(persons);
    } 

    private AssignBonusForm() {


    }

    public void setPersons(ArrayList<Person> persons) {
    AssignBonusForm.persons = persons;
    }

    public ArrayList<Person> getPersons() {
        return persons;
    }

MainForm form = new MainForm(persons);

   private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {                                        

        form.setVisible(true);

        this.dispose();
    }                                       

1 个答案:

答案 0 :(得分:4)

很难说格式不佳,但是,这对你来说有点奇怪吗?

public class MainForm
    ...
    AssignBonusForm assignFrame = new AssignBonusForm(payRolllist);

public class AssignBonusForm
    ...
    MainForm form = new MainForm(persons);

当您创建新的MainForm并创建新的AssignBonusForm时,您认为会发生什么?MainForm会创建新的AssignBonusForm,从而创建新的MainForm,从而创建新的AssignBonusForm MainForm创建了一个新的AssignBonusForm,用于创建新的MainForm,用于创建新的... at oca.project.MainForm.<init>(MainForm.java:91) at oca.project.AssignBonusForm.<init>(AssignBonusForm.java:80) at oca.project.MainForm.<init>(MainForm.java:22) ,用于创建新的MainForm,从而创建新的... {/ p>

您可以在堆栈跟踪中看到这一点:

bulk_create

为什么objs = [Model(id=1, field=foo), Model(id=2, field=bar)] 在那里两次?