Java类没有正确地从另一个类运行方法

时间:2017-01-30 19:20:01

标签: java class user-interface

首先,我为糟糕的头衔道歉。我很难描述这个错误。我有一个GUI应用程序,我正在使用以下类:Click here to see my classes.我遇到了一个非常奇怪的问题,可以在GUI中访问类设置但不能访问彩票。在GUI中,我可以通过我在GUI中创建的Settings对象调用方法,并且它可以工作。但是,当我从彩票中呼叫它时,返回任何内容或0。 我已在Lottery中创建了一个Settings对象。我在设置中的值之后单击按钮时运行以下代码。

public void initialize(){
    teams = settings.getTeams();
    pick = settings.getNumberOfTeams();
    //Returns nothing ^
    //teams and pick is a local varible
}

上面的变量什么都没有,即使我设置了它们。我知道它们已经被设置了,因为如果我从Settings类中调用了getTeams(),它将返回下面的正确值。

public void addTeam(String name, String wins, String losses){
    teams.add(new Team(name, wins, losses));
    System.out.println(teams.size() - 1 + " " + teams.get(teams.size() -   1));
    System.out.println("Number of teams (SETTINGS): " + getNumberOfTeams());
}

然而,当从彩票公司打电话时,它什么都没有。但是,当我从GUI调用Settings时,确实正确返回但没有任何其他类。注意:每个类都创建一个单独的Settings对象。 另一个有趣的事情是我的IDE说,该方法可以是私有的,即使我明确地从上面的另一个类中调用它。在此处查看:It's hard to see but Public is highlighted saying it can be private

我通常擅长发现错误,但是这个错误让我困扰了大约一个星期。什么都有帮助。如果有帮助,我还会将完整的彩票和设置课程放在下面。请注意:“设置”中的局部变量已设置,可以在“设置”中访问。

彩票类:

public class Lottery {
Settings settings = new Settings();

private Random rand = new Random();
private int randNum;
private int pick;
private ArrayList<Team> draftOrder = new ArrayList<Team>(0);
private ArrayList<Team> teams = new ArrayList<Team>(0);
private boolean lotteryDone;

public Lottery(){}

public void initialize(){
    teams = settings.getTeams();
    System.out.println("Lottery team size: " + teams.size());
    pick = settings.getNumberOfTeams();
    System.out.println("Number of Teams (From lottery): " + pick);
    System.out.println("Number of teams (LOTTO): " + pick);
    System.out.println("Test..." + settings.getDraftRounds());
    teams.add(settings.getTeam(1));
    System.out.println("Other method test: " + teams.get(0));
    }

public void setDraftOrder(){
    while (teams.size() > 0){
        randNum = rand.nextInt(teams.size() - 1);
        System.out.println(randNum);
        draftOrder.add(teams.remove(randNum));
        System.out.println(draftOrder.get(0));
    }
}

public void getNextPick(){
     if (pick > 0){
     randNum = rand.nextInt(teams.size() - 1);
     draftOrder.add(pick, teams.remove(randNum));
     pick -= 1;
     }
}

public int getPickNumber(){
    return pick;
}

public Team getCurrentPick(int i){
    return draftOrder.get(i);
}

public ArrayList<Team> getDraftOrder(){
    return draftOrder;
}

public boolean lotteryDone(){
    if (lotteryDone){
        return true;
    }else{
        return false;
    }
}

设置类:

public class Settings {
private ArrayList<Team> teams = new ArrayList<Team>(0);
private Boolean skipLottery;
private Boolean autoSelectPick;
private int autoSelectPickTimer;
private int totalMoney;
private int yellowOverlay;
private int draftRounds;
private int draftPickTimer;
private int draftTickTimer;
private int currentRound;
private int currentPick;
private boolean lotteryInitialized;
private boolean draftInitialized;
private boolean auctionInitialized;

public void addTeam(String name, String wins, String losses){
    teams.add(new Team(name, wins, losses));
    System.out.println(teams.size() - 1 + " " + teams.get(teams.size() - 1));
    System.out.println("Number of teams (SETTINGS): " + getNumberOfTeams());
}

public boolean settingsDone(){
    if (teams.size() != 0 && totalMoney != 0 && yellowOverlay != 0 && draftRounds != 0 && draftPickTimer != 0 && draftTickTimer != 0){
        return true;
    }else{
        return false;
    }
}

public void clearSettings(){
    teams.clear();
    skipLottery = false;
    autoSelectPick = false;
    autoSelectPickTimer = 0;
    totalMoney = 0;
    yellowOverlay = 0;
    draftRounds = 0;
    draftPickTimer = 0;
    draftTickTimer = 0;
    currentRound = 0;
    currentPick = 0;
    lotteryInitialized = false;
    draftInitialized = false;
    auctionInitialized = false;
}

public void setSkipLottery(boolean i){
    skipLottery = i;
}

public boolean getSkipLottery(){
    return skipLottery;
}

public void setAutoSelectPick(boolean i){
    autoSelectPick = i;
}

public boolean getAutoSelectPick(){
    return autoSelectPick;
}

public void setAutoSelectPickTimer(int i){
    autoSelectPickTimer = i;
}

public int getAutoSelectPickTimer(){
    return autoSelectPickTimer;
}

public void setTotalMoney(int i){
    totalMoney = i;
}

public int getTotalMoney(){
    return totalMoney;
}

public void setYellowOverlay(int i){
    yellowOverlay = i;
}

public int getYellowOverlay(){
    return yellowOverlay;
}

public void setDraftRounds(int i){
    draftRounds = i;
}

public int getDraftRounds(){
    return draftRounds;
}

public void setDraftPickTimer(int i){
    draftPickTimer = i;
}

public void setDraftTickTimer(int i){
    draftTickTimer = i;
}

public ArrayList<Team> getTeams(){
    return teams;
}

public Team getTeam(int index){
    return teams.get(index);
}

public int getNumberOfTeams(){
    return teams.size();
}

public int getDraftPickTimer(){
    return draftPickTimer;
}

public int getDraftTickTimer(){
    return draftTickTimer;
}

public void setLotteryInitialized(boolean i){
    lotteryInitialized = i;
}

public boolean getLotteryInitialized(){
    return lotteryInitialized;
}

public void setDraftInitialized(boolean i){
    draftInitialized = i;
}

public boolean getDraftInitialized(){
    return draftInitialized;
}

public void setAuctionInitialized(boolean i){
    auctionInitialized = i;
}

public boolean getAuctionInitialized(){
    return auctionInitialized;
}

1 个答案:

答案 0 :(得分:1)

&#34;注意:每个类,都会创建一个单独的设置对象。&#34; 这是导致问题的原因。

您有多个Setting类的实例。这意味着您无法访问在其他实例上设置的字段。

当你写作 Setting settings = new Settings(); 在彩票类中,您正在创建设置类的新实例。因此没有填充任何字段。

要解决此问题,您需要拥有一个集中的“设置”对象。只在连接所有内容(GUI?)和从该类写入/读取的类中创建一个Settings实例。您可以将设置的一个实例从GUI传递到Lottery类,它将保留您指定的所有字段。

此外,似乎您的Settings对象缺少构造函数,而您在Lottery.initialize()方法中的内容应该在(当前为空)构造函数中。您应该至少使用一般构造函数初始化您的设置。