2015-04-22 113 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