试图将arraylist传递给一种形式到另一种形式

时间:2017-12-16 07:07:56

标签: java inheritance

尝试将人物ArrayList从oca项目传递到主表单,然后将列表传递给assign bonus表单。它正确地传递给主窗体,但似乎无法在分配奖励中检索ArrayList。每次运行时,我都会在尝试获取1个人员列表时收到来自system.out部分的超出界限错误的错误。

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;
public class AssignBonusForm extends javax.swing.JFrame  {

  ArrayList<Person> persons = new ArrayList<>();
public AssignBonusForm(ArrayList<PayrollReportItem> payRolllist) {
        initComponents();

      System.out.println(persons.get(1));
    } 
private AssignBonusForm() {


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

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

1 个答案:

答案 0 :(得分:0)

我不完全确定这是问题(因为我不深入了解Java),但也许在MainForm中,在设置之前调用AssignBonusForm的构造函数列表?我不完全确定成员数据是先被初始化还是首先调用构造函数,但如果首先初始化成员数据,那么问题是当你调用成员数据时没有设置payRolllist的值AssignBonusForm构造函数。希望这会有所帮助:)