0

I am very confused by this. I set the condition "if (mSecondCount < mUsedItem.size())" but still received index out of bound exception. The line that exception is pointing at is "if (mUsedItem.get(mSecondCount).getItemNum() == 5)". And these codes are running on multiple devices. (Multiplayer realtime online game) but only one device was crashed. Please kindly help me to solve it

private void messageViewDisplayItemAfterEffect(Room fallenRoom) {
    Log.i(TAG, "Triggering after effect Items: " + mUsedItem);
    if (mUsedItem.size()>0){
        if (mSecondCount<mUsedItem.size()){
            disableContinue();
            mMessageView.setVisibility(View.VISIBLE);
            mMessageView.setEnabled(false);
            if (mSecondCount==0){
                mMessageView.setText("Items have been used, Some Items will triggered after affect");
            } else {
                mMessageView.setText("Loading Information");
            }
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (mUsedItem.get(mSecondCount).getItemNum()==5){
                        mMessageView.setText("The affect of hareware has expired, the blocked zombie has returned");
                        fallenRoom.zombieApproached();
                    } else if (mUsedItem.get(mSecondCount).getItemNum()==6){
                        mMessageView.setText("The affect of hidden has expired, the hidden character has returned");
                        fallenRoom.enter(gameBroad.matchGameCharacter(mPlayersUsedItem.get(mSecondCount),mUsedItem.get(mSecondCount).getAffectedGameCharacter().getName()));
                    } else if (mUsedItem.get(mSecondCount).getItemNum()==7){
                        mMessageView.setText("The affect of sprint has trigger, the left character has moved to the destination");
                        gameBroad.matchRoom(mUsedItem.get(mSecondCount).getAfteraffectedRoomNumber())
                                .enter(gameBroad.matchGameCharacter(mPlayersUsedItem.get(mSecondCount), mUsedItem.get(mSecondCount).getAffectedGameCharacter().getName()));
                    }
                    writeRoomIntoFireBase(fallenRoom);
                    updateRoom(MainActivity.this);
                    mMainActivityLayout.invalidate();
                    Handler handler1 = new Handler();
                    handler1.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            updateRoom(MainActivity.this);
                            mMainActivityLayout.invalidate();
                            mSecondCount++;
                            if (mMyPlayerID==getControlId()){
                                GameData gameData = new GameData(mCountPhase,mCountSetUp, mSecondCount,mThirdCount,mFourthCount,mFifthCount,mSixCount);
                                mDatabaseReference.child(GAMEDATA).setValue(gameData);
                                mDatabaseReference.child(TURN).setValue(-mSecondCount-1);
                            }
                        }
                    },DELAYEDSECONDSFORMESSAGEVIE*1000);
                }
            },DELAYEDSECONDSFORMESSAGEVIE*1000);
        } else if (mSecondCount==mUsedItem.size()){
            mMessageView.setText("Items after effect calculation finished");
            Handler handler1 = new Handler();
            handler1.postDelayed(new Runnable() {
                @Override
                public void run() {
                    updateRoom(MainActivity.this);
                    mMainActivityLayout.invalidate();
                    mFourthCount=5;
                    mCountSetUp=0;
                    mSecondCount=0;
                    mThirdCount=0;
                    if (mMyPlayerID==getControlId()){
                        GameData gameData = new GameData(mCountPhase,mCountSetUp, mSecondCount,mThirdCount,mFourthCount,mFifthCount,mSixCount);
                        mDatabaseReference.child(GAMEDATA).setValue(gameData);
                        mDatabaseReference.child(TURN).setValue(-20);
                    }

                }
            },DELAYEDSECONDSFORMESSAGEVIE*1000);
        }
    } else{
        mMessageView.setText("No Item will trigger after effect");
        Handler handler1 = new Handler();
        handler1.postDelayed(new Runnable() {
            @Override
            public void run() {
                updateRoom(MainActivity.this);
                mMainActivityLayout.invalidate();
                mFourthCount=5;
                mCountSetUp=0;
                mSecondCount=0;
                mThirdCount=0;
                if (mMyPlayerID == getControlId()){
                    GameData gameData = new GameData(mCountPhase,mCountSetUp, mSecondCount,mThirdCount,mFourthCount,mFifthCount,mSixCount);
                    mDatabaseReference.child(GAMEDATA).setValue(gameData);
                    mDatabaseReference.child(TURN).setValue(-40);
                }
            }
        },DELAYEDSECONDSFORMESSAGEVIE*1000);
    }
}

and exception are below

03-14 11:35:41.835 11603-11603/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.bignerdranch.android.mallofhorrorandroid, PID: 11603
                                               java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
                                                   at java.util.ArrayList.get(ArrayList.java:411)
                                                   at com.bignerdranch.android.mallofhorrorandroid.MainActivity$57.run(MainActivity.java:2268)
                                                   at android.os.Handler.handleCallback(Handler.java:754)
                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                   at android.os.Looper.loop(Looper.java:163)
                                                   at android.app.ActivityThread.main(ActivityThread.java:6342)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
Dexter.Z
  • 13
  • 1
  • 4
  • please note "disable continue" will not trigger anything on second count – Dexter.Z Mar 14 '18 at 19:15
  • `mSecondCount` value is 2, but your `mUsedItem` object only have two items. So index 2 doesn't exists ... – Thomas Mary Mar 14 '18 at 19:18
  • Hi Thomas, thanks for replaying. if mSecondCount reached value to 2, if should not have gone to that route. i set up the condition. That's my quesiton – Dexter.Z Mar 14 '18 at 19:24
  • The code throwing the exception is running "postDelayed" by some number of seconds - meaning 'mUsedItem' could have changed between the time it was checked and the time it was "postDelayed". –  Mar 14 '18 at 19:28
  • Thanks, Andy. I will set up another condition under postdelayed – Dexter.Z Mar 14 '18 at 20:31

0 Answers0