简体   繁体   中英

Array Index Out Of Bounds Exception Java Game

I'm creating a game in java and I'm attempting to create a multiplayer for it, when the game first starts, the level is loaded and the client is added to a playerlist which is clientside, the client will iterate through the list and add more if the server tells it to but the problem is that I'm getting 'ArrayIndexOutOfBoundsException' whent he game first starts, I've instructed my game to ignore it, I cant seem to fine the problem.

I'm assuming its something to do with creating new player and the time it takes for the threads to do their work but was hoping someone could help!

The message var is a String recieved fromt he server formatted like this:

name;address;posX;posY;fakeRotation;rotation;rotationSpeed;speed;sheildEnabled;sheildHealth;health

Here is an example of what the server sends:

Luke;/127.0.0.1:53701;50.0;50.0;0.0;0.0;0.0;0.0;true;100;100

Here are the vars:

String[] decrypted = message.split(",");
String[] commands = decrypted[x].split(";");

Here's the piece of code that's troubling me:

 boolean newPlayer = true;
        if(Main.level.playerList != null)
        {
            try
            {
                for(int p = 0; p < Main.level.playerList.size(); p++)
                {
                    if(Main.level.playerList.get(p).address.toString().equals(commands[1]))
                    {
                        newPlayer = false;
                        break;
                    }
                }

                if(newPlayer)
                {
                    Player player = new Player(50, 50, 100, 150, commands[1], Main.level.shipImage1, Main.level.thrusterFlamesSprite1, Main.level.shieldSprite1);
                    Main.level.playerList.add(player);
                }

                for(int p = 0; p < Main.level.playerList.size(); p++)
                {
                    Player player = Main.level.playerList.get(p);

                    if(player.address.equals(commands[1]))
                    {
                        // name:address:posX:posY:fakeRotation:rotation:rotationSpeed:speed:sheildEnabled:sheildHealth:health
                        player.name = commands[0];
                        player.address = commands[1];
                        player.pos[0] = Double.parseDouble(commands[2]);
                        player.pos[1] = Double.parseDouble(commands[3]);
                        player.fakeRotation = Float.parseFloat(commands[4]);
                        player.rotation = Float.parseFloat(commands[5]);
                        player.rotationSpeed = Double.parseDouble(commands[6]);
                        player.speed = Double.parseDouble(commands[7]);
                        player.sheildEnabled = Boolean.parseBoolean(commands[8]);
                        player.sheildStrength = Integer.parseInt(commands[9]);
                        //player.health = Integer.parseInt(commands[10]);
                    }
                }
            }
            catch(ArrayIndexOutOfBoundsException e)
            {
                System.out.println("Array index out of bounds, continuing.");

                e.printStackTrace();
            }
        }

Hopefully someone can help, like I said i think its somethign to do with adding a 'new' item but I can't find any way to make it work!

The stack trace I receive is:

java.lang.ArrayIndexOutOfBoundsException: 1
at multiplayer.Translator.interperate(Translator.java:28)
at multiplayer.GameClient.run(GameClient.java:81)

Thanks in advance!

According to the stack trace, is the error happening on the health line? If so, decrypted[x] may not have enough semicolons in it.

edit -

  String[] commands = decrypted[x].split(";");

So, what is x ? When you look at all decrypted[x] do you see what you expect? Is that value the 'Luke' data you posted earlier?

edit - also, I recommend you replace the 0 and 1 in pos[0] and pos[1] with static final constants:

  private static final int X_COORD = 0;
  private static final int Y_COORD = 1;
  /* ... snip ... */
  player.pos[X_COORD] = Double.parseDouble(commands[2]);
  player.pos[Y_COORD] = Double.parseDouble(commands[3]);

The point of this is to allow your code to refer to the X and Y coordinates by a descriptive constant instead of a number. While right now you know what 0 and 1 mean, a few months from now you won't, and when you look back ont he code you'll be mystified or confused. Adding the constants means you don't have to remember. It will also help you spot errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM