2014-09-29 132 views
-2

我在我的程序中得到一個堆棧溢出錯誤。我有一個功能和屬性的類,我需要我的部分類與WinForm的文本框,複選框等,才能夠訪問。當我建立它是好的,但我得到一個運行時錯誤。它指向我在Winform中使用的默認構造函數,就像Visual Studio聲稱堆棧溢出的地方一樣。 代碼:堆棧溢出錯誤。使用winforms

public class TeamCreator:Form 
    { 
     //fields 
     public string[] players=new string[12]; 
     public int[] points=new int[12]; 
     public int currentPlayerScore=0; 
     public TeamCreator()//default constructor 
     { 
      for (int i = 0; i < 11; i++) 
      { 
       this.players[i] = ""; 
       this.points[i] = 0; 
      } 

     } 
     public TeamCreator(string[] teammates,int[] scores)//construct Team object with user input 
     { 
      this.players = teammates; 
      this.points = scores; 

     } 
     public void setTeammates(string player,int index)//set players array 
     { 
      this.players[index] = player; 
     } 
     public void setPoints(int[] scoreList)//set points array 
     { 

       this.points = scoreList; 
     } 
     public void setPlayerScore(int playerScore,int playerNum)//sets a specific player's score 
     { 
      this.points[playerNum] = playerScore; 

     } 
     public int[] getPoints()//obtain array of points 
     { 
      int[] listOfPoints=new int[12]; 
      int i; 
      for(i=0;i<11;i++) 
      { 
       listOfPoints[i]=this.points[i]; 
      } 
      return listOfPoints; 
     } 
     public int totalPoints()//gets total points 
     { 
      int total=0; 
      for(int i=0;i<11;i++) 
      { 
       total = this.points[i] + total; 
      } 
      return total; 
     } 
     public double meanPoints()//returns mean or average of total points 
     { 
      int total = this.totalPoints(); 
      int mean = total/11; 
      return mean; 
     } 
    } 
} 

       //winform code 
       namespace TeamClass 
{ 
    public partial class TeamClass:TeamCreator 
    { 
     public int indexOn = 0; 
     public int current = 0; 
     public TeamCreator newTeam = new TeamCreator(); 
     public TeamClass() 
     { 
      InitializeComponent(); 
     } 

     private void playerInput_MouseLeave(object sender, EventArgs e)//adds players to players array and to list 
     { 
      string playerName = playerInput.Text; 
      newTeam.setTeammates(playerName,this.indexOn); 
      playerList.Items.Add(playerName); 
      indexOn++; 
     } 
     TeamClass reopen = new TeamClass(); 
     private void restart_CheckedChanged(object sender, EventArgs e)//allows user to restart program and make a new team 
     { 
      this.Visible = false; 
      reopen.Show(); 

     } 

     private void playerScoreDisplay_CheckedChanged(object sender, EventArgs e)//displays currently selected player when checked 
     { 
      string currentPlayerSelected = newTeam.players[current]; 
      MessageBox.Show("The current player selected is " + currentPlayerSelected + ".", "Current Player", MessageBoxButtons.OK); 
     } 

     private void playerList_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      this.current = playerList.SelectedIndex; 


     } 

     private void scoreInput_MouseLeave(object sender, EventArgs e)//gets player score 
     { 
      int currentScore; 

      if (!Int32.TryParse(scoreInput.Text, out currentScore)) 
      { 
       //tell user we can't parse the amount 
       if (MessageBox.Show("Text did not parse to an integer, please try again", "Invalid Argument", MessageBoxButtons.RetryCancel) == System.Windows.Forms.DialogResult.Cancel) 
       { 
        // leave method if they don't want to try again 
        return; 
       } 
       else 
       { 
        //set the focus on the control so user can fix error 
        scoreInput.Focus(); 
        //As a convenience select all text 
        scoreInput.SelectAll(); 
        //exit method 
        return; 
       } 
      } 

       newTeam.setPlayerScore(currentScore, current); 


     } 

     private void scoreInput_KeyPress(object sender, KeyPressEventArgs e)//makes sure numbers are being entered correctly for score 
     { 
      //only accept negative sign in first position 
      if ((e.KeyChar == '-') && ((sender as TextBox).Text.Length == 0)) 
      { 
       if ((sender as TextBox).Text.Contains("-")) 
        e.Handled = true; 
      } 
      //Only accept numbers, one decimal, one negative sign (-) and the backspace 
      else if (!char.IsDigit(e.KeyChar) && !char.IsPunctuation(e.KeyChar) && !(e.KeyChar == 0x08)) 
      { 
       e.Handled = true; 
      } 
     } 

     private void totalPointsDisplay_CheckedChanged(object sender, EventArgs e)//displays total points when checked 
     { 
      int total=newTeam.totalPoints(); 
      MessageBox.Show("The total points for the team is " + total + ".", "Total Points", MessageBoxButtons.OK); 
     } 

     private void MeanPointsDisplay_CheckedChanged(object sender, EventArgs e)//displays mean points when checked 
     { 
      double avg=newTeam.meanPoints(); 
      MessageBox.Show("The mean points for the team is " + avg + ".", "Mean Points", MessageBoxButtons.OK); 

     } 

    } 
    } 

任何幫助,爲什麼我收到此錯誤,以及如何解決這將是大加讚賞。我對C#有點新,所以我不確定問題是什麼。

+1

開始通過查看整個異常信息,包括堆棧跟蹤。 – Blorgbeard 2014-09-29 01:03:33

回答

2

不幸的是,你可能無法從異常中分辨出多少。相反,一個有用的堆棧跟蹤,你會看到這樣的消息:

因爲當前線程堆棧溢出狀態無法計算表達式

周圍有一些這方面的方法,如果你做一個搜索「stackoverflow exception stack trace」。

但是,根本問題幾乎總是遞歸,所以尋找自己調用的東西,或者在循環中調用另外兩個或三個方法。

快速掃描您的代碼後,我幾乎立即發現了一個(也可能有其他人)。每當你實例化一個TeamClass,你就創建另一個實例,創建另一個實例,並且繼續。

public partial class TeamClass:TeamCreator 
{ 
    ... 
    TeamClass reopen = new TeamClass(); 
    ... 
+0

謝謝。我現在有我的程序正常工作。 – arisonu123 2014-09-29 03:45:11

0

任何你在你的代碼中使用數組中第11個元素的原因? 您定義與12元素的數組,但你總是循環的11要素

public string[] players=new string[12]; 
     public int[] points=new int[12]; 
     public int currentPlayerScore=0; 
     public TeamCreator()//default constructor 
     { 
      here why you loop to 11 it suppose to be 12 or the array.Length 
      for (int i = 0; i < 11; i++) 
      { 
       this.players[i] = ""; 
       this.points[i] = 0; 
      } 
+0

我以爲數組從0到1的大小都小於0-11。如果我實際上沒有訪問它們,那麼會少於12或少於或等於11解決這個問題? – arisonu123 2014-09-29 02:09:00

+0

是的,我<= 11將解決這個問題。數組的大小爲12.如果i小於11,則條件爲停止循環,因此數組中的最後一個元素將被忽略。 – 2014-09-29 02:15:49

+0

還有一件事 TeamClass reopen = new TeamClass(); 應該是私有的方法 private void restart_CheckedChanged(object sender,EventArgs e) – 2014-09-29 02:20:38