2015-04-22 57 views
-1

所以在这个类中选择== 1,它不会通过对象的ArrayList进入for循环,它是否意味着对象是空的?因为我在开始时声明了对象并将它们添加到ArrayList存储中。Arraylist of objects empty?

public static void main(String[] args) { 
     ArrayList<Team> store = new ArrayList<>(); 
     Random gener = new Random(); 
     String tourName , tourDate , location; 
     int maxNumberofTeams , avalSoft, avalHard , avalFieldTest; 
     Scanner input = new Scanner(System.in); 
     System.out.print("Please Enter tournament Name?\n"); 
     tourName = input.next(); 
     System.out.print("please Enter tournament Date\n"); 
     tourDate = input.next(); 
     System.out.print("please Enter location\n"); 
     location = input.next(); 
     System.out.print("Please Enter Max number of Teams\n"); 
     maxNumberofTeams = input.nextInt(); 
     System.out.print("Please Enter avalSoft\n"); 
     avalSoft = input.nextInt(); 
     System.out.print("Please enter aval Hard\n"); 
     avalHard = input.nextInt(); 
     System.out.print("Please Enter avalFieldTest\n"); 
     avalFieldTest = input.nextInt(); 
     Tournament tour = new Tournament (tourName , tourDate, location , maxNumberofTeams, avalSoft , avalHard, avalFieldTest); 
     for (int i = 1 ; i <= maxNumberofTeams ; i++) 
     { 
      String teamName , sponsoringSchool , financialSponsor , judgeLocation; 
      int teamNumber , noOfTeamMem , robotId; 
      System.out.print("Please Enter %s team Name\n"); 
      teamName = input.next(); 
      System.out.print("Number of Team Memebers\n"); 
      noOfTeamMem = input.nextInt(); 
      System.out.print("Please Enter Sponsoring Schoolr\n"); 
      sponsoringSchool = input.next(); 
      System.out.print("Please Enter financialSponsor\n"); 
      financialSponsor = input.next(); 
      System.out.print("Please Enter judge Location\n"); 
      judgeLocation = input.next(); 
      teamNumber = i; 
      System.out.print("Please Enter an ID for robot\n"); 
      robotId = input.nextInt(); 
      Robot robbb = new Robot(); 
      Team team = new Team(teamName , teamNumber , noOfTeamMem , sponsoringSchool , financialSponsor, judgeLocation, robbb); 
      Robot rob = new Robot (team , robbb); 
      store.add(team); 
     } 
     int choice=0; 
     while(choice >= 0) 
      { 
      System.out.print("MENU\n" 
        + "1)PREPARING TEAM TURN ROBOT ON\n" 
        +"2)PREPARING TEAM HARDWARE\n" 
        + "3)HAVE HW INSPECT READY ROBOT\n" 
        + "4)HAVE PRERPARING TEAM TAKE HW_INSPECTED ROBOT TO STATION\n" 
        + "5)HAVE SW INSPECT ROBOT\n" 
        + "6)HAVE A PREPARING TEAM TAKE TO FIELD TEST\n" 
        + "7)HAVE A FIELD TEST INSPEC\n" 
        + "8)HAVE A BEFORE TEAM GO TO JUDGE\n" 
        + "9)HAVE JUDGES INTERVIEW\n" 
        + "10)CHANGE TEAM STATUS TO PASSED_INSPECTION\n" 
        + "11)TOURNAMENT STATUS TO MATCH\n" 
        + "12)\n" 
        + "13)ROBOT CAN'T PLAY IF OFF, READY, OR STILL AT TESTING\n" 
        + "14)CHANGE TOURNAMENT TO MATCHES\n" 
        + "15)GENERATE POINTS\n" 
        + "16)JUDGE POINTS\n" 
        + "17)CHANGE TOURNAMENT TO AWARDS\n" 
        + "18)PRINT TOP TEAMS JUDGING\n" 
        + "19) PRINT TOP BY QULIFYING \n" 
        + "20)DISPLAY TEAM PERSONS\n" 
        + "21)DISPLAY TEAM INFO\n" 
        + "22)DISPLAY INFO ABOUT ROBOTS\n" 
        + "23)INFO ABOUT TOURNAMENT\n" 
        + "24)END\n"); 

      choice = input.nextInt(); 
      Team temp = new Team(); 
      Robot robottemp = new Robot(); 
      Tournament tourr = new Tournament(); 
      if (choice == 1) 
      { 
       System.out.print("Phase"+choice); 
       for (int i = 0 ; i >= store.size(); i++) 
       { 
        System.out.print("Phase"+choice); 
        store.get(i).teamStatus = temp.teamStatus.PREPARING; 
        if (store.get(i).teamStatuss == 1) 
        { 
        store.get(i).robot.robotStatusChoice(1); 
        System.out.print("PHASE 1 COMPLETED\n"); 
        } 
       } 
      } 

这是我在其中调用该方法的机器人类;

public int teamNumber; 
    public int robotId; 
    public Robot robot; 
    Random gener = new Random(); 
    public int robotStatusChoice; 
    ArrayList stations = new ArrayList(); 
    public int Height; 
    public int Width = 22; 
    public int Depth; 
    public Team TeamAssigned = new Team("TEAMNAME", 1 , 1, "SS", "FS", "JL",robot); 
    public robotStatus robotStatus; 

    public Robot() 
    { 

    } 
    public Robot(Team TeamAssigned ,Robot robot){ 
     this.robot = robot; 
     this.TeamAssigned = TeamAssigned; 
    } 


public void robotStatusChoice(int i) { 
      if (i == 1) 
      { 
       this.robotStatusChoice = i; 
       this.robotStatus = robotStatus.READY; 
      } 
      else if(i == 2) 
      { 
       this.robotStatusChoice = i; 
       this.robotStatus = robotStatus.HW_INSP_PASSED; 
      } 
      else if (i == 3) 
      { 
       this.robotStatusChoice = i; 
       this.robotStatus = robotStatus.FIELD_TEST_PASSED; 
      } 
      else if (i > 3 || i < 1) 
      { 
       this.robotStatusChoice = i; 
       this.robotStatus = robotStatus.READY; 
      } 
     } 

TeamSTatus Change;

public void TeamStatus(int x) 
    { 
     if(x == 1) 
     { 
      this.teamStatuss = x; 
      this.teamStatus = teamStatus.PREPARING; 
     } 
     else if (x == 2) 
     { 
      this.teamStatuss = x; 
      this.teamStatus = teamStatus.PASSED_INSPECTION; 
     } 
     else if (x == 3) 
     { 
      this.teamStatuss = x; 
      this.teamStatus = teamStatus.PLAYED5_MATCHES; 
     } 
     else if (x == 4) 
     { 
      this.teamStatuss = x; 
      this.teamStatus = teamStatus.INELIGIBLE; 
     } 
     else if (x > 4 || x<1) 
     { 
      this.teamStatuss = x; 
      this.teamStatus = teamStatus.INELIGIBLE; 
     } 


    } 
+1

请发布*短*,但完整的程序来演示问题。我怀疑你发布的代码很少有相关的 - 但它也不完整。 –

+0

你可以发布所有修改'Team.teamStatus'的代码吗? –

+0

@Ankosh是否有'enum teamStatus'?另请注意,方法名称应以小写字母[a-z]开头。将方法名从'TeamStatus'更改为'setTeamStatus',并将'store.get(i).teamStatus = temp.teamStatus.PREPARING'更改为'store.get(i).setTeamStatus(1)'。 –

回答

1

在末你choice == 1块,你有你的for循环条件倒退。当条件为true而不是false时,for循环的下一次迭代发生。逆转你的状况。

for (int i = 0 ; i < store.size(); i++) 
+0

我做到了,但它给了我“Java.lang.NullPointerException”错误? @rgettman – Ankosh

+0

@Ankosh看起来你已经发现了程序中的另一个bug。请检查您的堆栈跟踪以确定NPE正在发生什么行。这会告诉你什么对象是空的,你试图调用一个方法。不知道这条线,我们只能猜测。 – rgettman

+0

我更新了帖子并添加了从Robot类中使用的方法。@rgettman – Ankosh

1

这就是问题所在:

for (int i = 0 ; i >= store.size(); i++) 

除非store是空的(在这种情况下,你会得到一个例外),i >= store.size()将立即false。您的意思是:

for (int i = 0; i < store.size(); i++) 

或者更好的是,使用循环增强:

for (Team team : store) { 
    team.teamStatus = temp.teamStatus.PREPARING; 
    if (team.teamStatuss == 1) { 
     team.robot.robotStatusChoice(1); 
     System.out.print("PHASE 1 COMPLETED\n"); 
    } 
} 

(你真的有两个teamStatusteamStatuss作为字段这听起来像一个好主意吗? 。)

+0

我做到了,但它给了我“Java.lang.NullPointerException”错误? @JonSkeet – Ankosh

+2

@Ankosh:那么这是你需要修复的另一个问题。我建议你阅读http://tinyurl.com/so-npe,并努力修复它,在尝试自己修复它之后再询问另一个问题(仅包含相关代码)*。堆栈溢出不是针对单个问题设计的,而是最终成为一种慢动作交互式调试会话。 –

0
for (int i = 0 ; i >= store.size(); i++) 

这可能是一个错字,你可能意味着i < store.size()

在你的代码有:

Robot robbb = new Robot(); 
Team team = new Team(teamName , teamNumber , noOfTeamMem , sponsoringSchool , financialSponsor, judgeLocation, robbb); 
Robot rob = new Robot (team , robbb); 

现在,你传递一个不正确实例Robot()robbb)至Team构造。创建一个setRobot(Robot robot)方法来更新Team的机器人到rob,这是一个Robot参考Team之前将Team添加到List

public void setRobot(Robot rob) { this.robot = rob; }; 
+0

我做到了,但它给了我“Java.lang.NullPointerException”错误? @M。 Shaw – Ankosh

+0

你可以发布stacktrace并更新你的文章中的代码? –

+0

已更新。谢谢。 @ M.Shaw – Ankosh