title: How to prevent StackOverflowError in minesweeper program?
body: <p>I made a program designed to run like the game Minesweeper.  It is set up as a GridLayout and there is a recursive code called getNumberSmall in the main code, that runs when a button is clicked.  It doesn't run recursively unless the button clicked was a blank space (i.e. if you have ever played minesweeper neither a mine or a number).  When a BLANK space is clicked the code is supposed to recursively run itself on the buttons surrounding the blank button that was clicked.  HOwever, when I do it I get a stack overflow error.  How should I fix this?</p>
  
  <pre><code>public void getNumberSmall(JButton b3){
      JButton above = new JButton();
      JButton below = new JButton();
      JButton right = new JButton();
      JButton left = new JButton();
      JButton DRight = new JButton();
      JButton DLeft = new JButton();
      JButton DRightD = new JButton();
      JButton DLeftD = new JButton();
      JButton clicked = b3;
      int myX = (int) clicked.getClientProperty("column");
      int myY = (int) clicked.getClientProperty("row");
      int mNum = 0;
  
      if(myX &lt; 9){
          for(int i = 0; i &lt; small.size(); i++){
              if((int) small.get(i).getClientProperty("column") - 1 == myX){
                  if((int) small.get(i).getClientProperty("row") == myY){
                      right = small.get(i);
                      if((int)right.getClientProperty("enabled") == 1){
                          right = null;
                      }
                  }
  
              }
          }
      }
      else{
          right = null;
      }
  
      if(myX &gt; 1){
          for(int i = 0; i &lt; small.size(); i++){
              if((int) small.get(i).getClientProperty("column") + 1 == myX){
                  if((int) small.get(i).getClientProperty("row") == myY){
                      left = small.get(i);
                      if((int)left.getClientProperty("enabled") == 1){
                          left = null;
                      }
                  }
  
              }
          }
      }
      else{
          left = null;
      }
  
      if(myY &lt; 9){
          for(int i = 0; i &lt; small.size(); i++){
              if((int) small.get(i).getClientProperty("column") == myX){
                  if((int) small.get(i).getClientProperty("row") - 1 == myY){
                      below = small.get(i);
                      if((int)below.getClientProperty("enabled") == 1){
                          below = null;
                      }
                  }
  
              }
          }
      }
      else{
          below = null;
      }
  
      if(myY &gt; 1){
          for(int i = 0; i &lt; small.size(); i++){
              if((int) small.get(i).getClientProperty("column") == myX){
                  if((int) small.get(i).getClientProperty("row") + 1 == myY){
                      above = small.get(i);
                      if((int)above.getClientProperty("enabled") == 1){
                          above = null;
                      }
                  }
  
              }
          }
      }
      else{
          above = null;
      }
  
      if(myY == 9){
          DRightD = null;
          DLeftD = null;
      }
      if(myY == 1){
          DRight = null;
          DLeft = null;
      }
      if(myX == 9){
          DRightD = null;
          DRight = null;
      }
      if(myX == 1){
          DLeftD = null;
          DLeft = null;
      }
      if(DRight != null){
          for(int i = 0; i &lt; small.size(); i++){
              if((int) small.get(i).getClientProperty("column") - 1 == myX){
                  if((int) small.get(i).getClientProperty("row") + 1 == myY){
                      DRight = small.get(i);
                      if((int)DRight.getClientProperty("enabled") == 1){
                          DRight = null;
                      }
                  }
  
              }
          }
      }
  
      if(DLeft != null){
          for(int i = 0; i &lt; small.size(); i++){
              if((int) small.get(i).getClientProperty("column") + 1 == myX){
                  if((int) small.get(i).getClientProperty("row") + 1 == myY){
                      DLeft = small.get(i);
                      if((int)DLeft.getClientProperty("enabled") == 1){
                          DLeft = null;
                      }
                  }
  
              }
          }
      }
  
      if(DRightD != null){
          for(int i = 0; i &lt; small.size(); i++){
              if((int) small.get(i).getClientProperty("column") - 1 == myX){
                  if((int) small.get(i).getClientProperty("row") - 1 == myY){
                      DRightD = small.get(i);
                      if((int)DRightD.getClientProperty("enabled") == 1){
                          DRightD = null;
                      }
                  }
  
              }
          }
      }
  
      if(DLeftD != null){
          for(int i = 0; i &lt; small.size(); i++){
              if((int) small.get(i).getClientProperty("column") + 1 == myX){
                  if((int) small.get(i).getClientProperty("row") - 1 == myY){
                      DLeftD = small.get(i);
                      if((int)DLeftD.getClientProperty("enabled") == 1){
                          DLeftD = null;
                      }
                  }
  
              }
          }
      }
  
      if(above != null){
          if((int) above.getClientProperty("mines") == 1){
              mNum = mNum + 1;
          }
      }
      if(below != null){
          if((int) below.getClientProperty("mines") == 1){
              mNum = mNum + 1;
          }
      }
      if(right != null){
          if((int) right.getClientProperty("mines") == 1){
              mNum = mNum + 1;
          }
      }
      if(left != null){
          if((int) left.getClientProperty("mines") == 1){
              mNum = mNum + 1;
          }
      }
      if(DRight != null){
          if((int) DRight.getClientProperty("mines") == 1){
              mNum = mNum + 1;
          }
      }
      if(DLeft != null){
          if((int) DLeft.getClientProperty("mines") == 1){
              mNum = mNum + 1;
          }
      }
      if(DRightD != null){
          if((int) DRightD.getClientProperty("mines") == 1){
              mNum = mNum + 1;
          }
      }
      if(DLeftD != null){
          if((int) DLeftD.getClientProperty("mines") == 1){
              mNum = mNum + 1;
          }
      }
  
      if(mNum == 0){
          String num2 = "" + mNum;
          clicked.setText(num2);
  
          if((above != null) &amp;&amp; ((int)above.getClientProperty("enabled") == 0)){
          getNumberSmall(above);
          above.putClientProperty("enabled", 1);
          }
  
          if((below != null) &amp;&amp; ((int)below.getClientProperty("enabled") == 0)){
          getNumberSmall(below);
          below.putClientProperty("enabled", 1);
          }
  
          if((right != null) &amp;&amp; ((int)right.getClientProperty("enabled") == 0)){
          getNumberSmall(right);
          right.putClientProperty("enabled", 1);
          }
  
          if((left != null )  &amp;&amp; ((int)left.getClientProperty("enabled") == 0)){
          getNumberSmall(left);
          left.putClientProperty("enabled", 1);
  
          }
  
          if((DRight != null)  &amp;&amp; ((int)DRight.getClientProperty("enabled") == 0)){
          getNumberSmall(DRight);
          DRight.putClientProperty("enabled", 1);
  
          }
  
          if((DLeft != null)  &amp;&amp; ((int)DLeft.getClientProperty("enabled") == 0)){
          getNumberSmall(DLeft);
          DLeft.putClientProperty("enabled", 1);
  
          }
  
          if((DRightD != null)  &amp;&amp; ((int)DRightD.getClientProperty("enabled") == 0)){
          getNumberSmall(DRightD);
          DRightD.putClientProperty("enabled", 1);
  
          }
  
          if((DLeftD != null)  &amp;&amp; ((int)DLeftD.getClientProperty("enabled") == 0)){
          getNumberSmall(DLeftD);
          DLeftD.putClientProperty("enabled", 1);
  
          }
  
  
      }
  
      else{
      String number = "" + mNum;
      clicked.setText(number);
      }
  }
  </code></pre>