因此,我正在申请工资单,但遇到了问题。每次我运行它时,都会遇到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();
    }

最佳答案

格式不佳很难说出来,但是,这对您来说似乎有点奇怪吗?

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在那里两次?

关于java - 使用类实例的stackoverflow错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48016065/

10-10 09:54