0

I'm writing a game on Android platform. I have array of buttons and when I click in one of them, it will show up "X" on it and neighbouring buttons like this:

  X
X X X
  X

But when I click one of the border buttons application is stopping.

This is my method responsible for setting text on buttons (if you need more code, please tell me):

private void changeState(int locationVer, int locationHor){
    String buttonText = BoardButtons[locationVer][locationHor].getText().toString();
    String rightButtonText = BoardButtons[locationVer+1][locationHor].getText().toString();
    String leftButtonText = BoardButtons[locationVer-1][locationHor].getText().toString();
    String lowerButtonText = BoardButtons[locationVer][locationHor+1].getText().toString();
    String upperButtonText = BoardButtons[locationVer][locationHor-1].getText().toString();
    if(locationVer == 0) {
        if (buttonText == String.valueOf(Game.PLAYER)) {
            BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));

            if (rightButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (rightButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (lowerButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (lowerButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.PLAYER)));

            if (upperButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (upperButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.PLAYER)));
        }


        if (buttonText == String.valueOf(Game.EMPTY_SPACE)) {
            BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (rightButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (rightButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (lowerButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (lowerButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.PLAYER)));

            if (upperButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (upperButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.PLAYER)));
        }
    }

    else if(locationVer == 6) {
        if (buttonText == String.valueOf(Game.PLAYER)) {
            BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));

            if (leftButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (leftButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (lowerButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (lowerButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.PLAYER)));

            if (upperButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (upperButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.PLAYER)));
        }


        if (buttonText == String.valueOf(Game.EMPTY_SPACE)) {
            BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (leftButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (leftButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (lowerButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (lowerButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.PLAYER)));

            if (upperButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (upperButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.PLAYER)));
        }
    }

    else if(locationHor == 0) {
        if (buttonText == String.valueOf(Game.PLAYER)) {
            BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));

            if (rightButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (rightButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (leftButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (leftButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (lowerButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (lowerButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.PLAYER)));
        }

        if (buttonText == String.valueOf(Game.EMPTY_SPACE)) {
            BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (rightButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (rightButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (leftButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (leftButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (lowerButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (lowerButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.PLAYER)));
        }
    }

    else if(locationHor == 6) {
        if (buttonText == String.valueOf(Game.PLAYER)) {
            BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));

            if (rightButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (rightButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (leftButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (leftButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (upperButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (upperButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.PLAYER)));
        }

        if (buttonText == String.valueOf(Game.EMPTY_SPACE)) {
            BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (rightButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (rightButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (leftButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (leftButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (upperButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (upperButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.PLAYER)));
        }
    }

    else {
        if (buttonText == String.valueOf(Game.PLAYER)) {
            BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));

            if (rightButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (rightButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (leftButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (leftButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (lowerButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (lowerButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.PLAYER)));

            if (upperButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (upperButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.PLAYER)));
        }


        if (buttonText == String.valueOf(Game.EMPTY_SPACE)) {
            BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (rightButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (rightButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (leftButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (leftButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.PLAYER)));

            if (lowerButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (lowerButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.PLAYER)));

            if (upperButtonText == String.valueOf(Game.PLAYER))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.EMPTY_SPACE)));
            if (upperButtonText == String.valueOf(Game.EMPTY_SPACE))
                BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.PLAYER)));
        }
    }
}

This is what logcat shows:

06-06 17:13:56.727    2232-2232/pl.edu.uksw.fieldsgame E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: pl.edu.uksw.fieldsgame, PID: 2232
java.lang.ArrayIndexOutOfBoundsException: length=7; index=7
        at pl.edu.uksw.fieldsgame.MainActivity.changeState(MainActivity.java:149)
        at pl.edu.uksw.fieldsgame.MainActivity.access$200(MainActivity.java:13)
        at pl.edu.uksw.fieldsgame.MainActivity$ButtonClick.onClick(MainActivity.java:137)
        at android.view.View.performClick(View.java:4780)
        at android.view.View$PerformClick.run(View.java:19866)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
groniu
  • 13
  • 4
  • It would be useful to know what line it is crashing on. Include the complete stack trace when you mention things like the `"application is stopping"`. – trooper Jun 06 '15 at 14:45
  • also, see http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java (probably unrelated to your question, but its an obvious problem that jumps out). – trooper Jun 06 '15 at 14:48
  • No, it's not because compare strings. I thing lines like "if(locationVer == 0)" doesn't working. I will edit my question and show logcat – groniu Jun 06 '15 at 15:15
  • The error should give you a hint ;) The indices of an array in Java run from 0 to length-1, so array[length] is actually out of bounds by 1. It is likely that you are passing a value of `6` as one of the parameters of your function - the first thing you do is `locationVer+1` which would cause the exception. Checking the bounds of locationVer and locationHor upfront might go a long way to fix things. – trooper Jun 06 '15 at 15:26
  • Also, please research the difference between `==` and `equals`. I realize its not the source of your current problem, but what you have coded there is just wrong (really asking for trouble). – trooper Jun 06 '15 at 15:27

1 Answers1

0

ArrayIndexOutOfBoundsException means that you're trying to read an index that isn't in the array, in this case you have a length 7 array (indices 0 through 6) but you're asking for position 7, which doesn't exist.

I don't have enough data to align line numbers with your code, but I'm guessing this is coming from this line:

String rightButtonText = BoardButtons[locationVer+1][locationHor].getText().toString();

You don't check to ensure that locationVer+1 is a valid array index before trying to read it out of the array. It looks like your following code will handle rightButtonText being null, so this should work (repeat for each array read, changing the condition as appropriate for the array index that's being incremented/decremented):

String rightButtonText = null;
if (locationVer + 1 < BoardButtons.length) {
    rightButtonText = BoardButtons[locationVer+1][locationHor].getText().toString();
}

Edit:

The next problem you'll have is using == vs .equals() for comparing strings. In java the == operator used on objects (not primitives) compares references, not values (see here including answers for more explanation).

When you call if (buttonText == String.valueOf(Game.PLAYER)) you're asking java if buttonText is the same string (as in "is the same reference", not "has the same value") as String.valueOf(Game.PLAYER). This will return false every time because String.valueOf(Game.PLAYER) is creating a new string. To actually do the comparison you thought you were getting, swap this around to:

if (String.valueOf(Game.PLAYER).equals(buttonText))
Community
  • 1
  • 1
1337joe
  • 2,327
  • 1
  • 20
  • 25
  • Ok, it's almost correctly. I can click border buttons but when I'm clicking any button it only sets "X" on middle (clicked), lower and right button. How to fix this? – groniu Jun 06 '15 at 16:48
  • Now you're finally hitting the problem with using `==` vs `.equals(). I'll edit this into my answer for the sake of formatting. – 1337joe Jun 06 '15 at 17:12
  • Noooo, it didn't solve this new problem. It sets only 3 "X" again. – groniu Jun 06 '15 at 17:23
  • Ok, I finally found the mistake! I had if (locationHor - 1 >= BoardButtons.length) instead of if (locationHor - 1 >= 0)... – groniu Jun 06 '15 at 20:26