索引越界异常

时间:2011-06-06 12:31:14

标签: java indexoutofboundsexception

game.getS().getVehicles().get(1).updatePosition(positions.get(0));

我们正在制作一个客户端 - 服务器赛车游戏,我们在这个确切的行中得到了这个例外我们可以做什么或者我们可以改变什么?在这里,我将向您展示所有代码:

package speedrace.client;

import static speedrace.common.Config.*;

import speedrace.common.*;
import speedrace.game.Car;
import speedrace.game.CarGame;
import speedrace.game.Player;

import java.awt.geom.Point2D;
import java.awt.geom.Point2D.Float;
import java.rmi.*;
import java.rmi.server.*;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.*;

public class SpeedRaceClientController extends UnicastRemoteObject implements
        RaceObserver, RaceUpdate
{
    private RaceSubject serverGet;
    private RaceUpdate serverSet;
    private SpeedRaceClientView view;

    private int clientID = 0;
    private Player currentPlayer = null;
    private CarGame game = new CarGame();

    public SpeedRaceClientController() throws Exception
    {
        super();
        game.setVisible(false);
    }

    @Override
    public Player getCurrentPlayer() throws RemoteException
    {
        return currentPlayer;
    }

    @Override
    public void setCurrentPlayer(Player currentPlayer) throws RemoteException
    {
        this.currentPlayer = currentPlayer;

    }

    @Override
    public int getClientID()
    {
        return clientID;
    }

    @Override
    public void setClientID(int clientID)
    {
        this.clientID = clientID;
    }

    @Override
    public CarGame getGame()
    {
        return game;
    }

    @Override
    public void setGame(CarGame game)
    {
        this.game = game;
    }

    public void begin(SpeedRaceClientView view)
    {
        this.view = view;

        try
        {
            String ip = view.inputIP();
            String URL = "rmi://" + ip + "/" + SERVICE;

            serverSet = (RaceUpdate) Naming.lookup(URL);
            serverGet = (RaceSubject) Naming.lookup(URL);

            serverGet.attach(this);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            view.error("Trouble connecting to server");

            System.exit(1);
        }
    }

    @Override
    public void updatePlayerList(Vector<Player> playersList)
            throws RemoteException
    {
        view.showOnlinePlayers(playersList);

    }

    @Override
    public void registerNewPlayer(Player player, int clientID)
            throws RemoteException
    {
        try
        {
            serverSet.registerNewPlayer(player, clientID);
        }
        catch (RemoteException ex)
        {
            ex.printStackTrace();
            view
                    .error("Trouble connecting to server. Please restart the server.");
            System.exit(1);
        }

    }

    @Override
    public void startGame() throws RemoteException
    {

        try
        {
            serverSet.startGame();

        }

        catch (RemoteException re)
        {
            re.printStackTrace();
            view
                    .error("Trouble connecting to server. Please restart the server.");
            System.exit(1);
        }
    }

    @Override
    public void startTheGame(ArrayList<Point2D.Float> positions,
            Vector<RaceObserver> observers) throws RemoteException
    {

        switch (clientID)
        {
            case 1:
                game.updatePosition(273, 471);
                break;
            case 2:
                game.updatePosition(315, 408);
                break;
            case 3:
                game.updatePosition(353, 471);
                break;
            case 4:
                game.updatePosition(395, 408);
                break;
            case 5:
                game.updatePosition(433, 471);
                break;
            case 6:
                game.updatePosition(475, 408);
                break;
            case 7:
                game.updatePosition(513, 471);
                break;
            case 8:
                game.updatePosition(555, 408);
                break;
        }

        for (int i = 1; i < observers.size(); i++)
        {
            game.getS().getVehicles().add(
                    new Car(getClass().getResource(
                            "/speedrace/game/bluecar.png")));

            game.getS().getVehicles().get(i).setMass(0.9f);
            game.getS().getVehicles().get(i).setMaxSteering(500.0f);
            game.getS().getVehicles().get(i).updateVelocity(80.0f, 0.0f);
        }
        updatePosition(clientID, positions);

        game.setVisible(true);
        game.getS().requestFocusInWindow();
        sendCurrentCarPosition(game.getS().getVehicles().get(0).getPosition(),clientID);
    }

    public void updatePosition(int id, ArrayList<Point2D.Float> positions){
        if(id==1){
            for (int i = 1; i < game.getS().getVehicles().size(); i++)
            {
                game.getS().getVehicles().get(i).updatePosition(positions.get(i));              
            }

        }else if(id==2){
            game.getS().getVehicles().get(1).updatePosition(positions.get(0));              
            for (int i = 2; i < game.getS().getVehicles().size(); i++)
            {
                game.getS().getVehicles().get(i).updatePosition(positions.get(i));  
            }
        }else if(id==3){
            game.getS().getVehicles().get(1).updatePosition(positions.get(0));  
            game.getS().getVehicles().get(2).updatePosition(positions.get(1));
            for (int i = 3; i < game.getS().getVehicles().size(); i++)
            {
                game.getS().getVehicles().get(i).updatePosition(positions.get(i));  
            }
        }else if(id==4){
            game.getS().getVehicles().get(1).updatePosition(positions.get(0));  
            game.getS().getVehicles().get(2).updatePosition(positions.get(1));
            game.getS().getVehicles().get(3).updatePosition(positions.get(2));
            for (int i = 4; i < game.getS().getVehicles().size(); i++)
            {
                game.getS().getVehicles().get(i).updatePosition(positions.get(i));  
            }
        }else if(id==5){
            game.getS().getVehicles().get(1).updatePosition(positions.get(0));  
            game.getS().getVehicles().get(2).updatePosition(positions.get(1));
            game.getS().getVehicles().get(3).updatePosition(positions.get(2));
            game.getS().getVehicles().get(4).updatePosition(positions.get(3));
            for (int i = 5; i < game.getS().getVehicles().size(); i++)
            {
                game.getS().getVehicles().get(i).updatePosition(positions.get(i));  
            }
        }else if(id==6){
            game.getS().getVehicles().get(1).updatePosition(positions.get(0));  
            game.getS().getVehicles().get(2).updatePosition(positions.get(1));
            game.getS().getVehicles().get(3).updatePosition(positions.get(2));
            game.getS().getVehicles().get(4).updatePosition(positions.get(3));
            game.getS().getVehicles().get(5).updatePosition(positions.get(4));
            for (int i = 6; i < game.getS().getVehicles().size(); i++)
            {
                game.getS().getVehicles().get(i).updatePosition(positions.get(i));  
            }
        }else if(id==7){
            game.getS().getVehicles().get(1).updatePosition(positions.get(0));  
            game.getS().getVehicles().get(2).updatePosition(positions.get(1));
            game.getS().getVehicles().get(3).updatePosition(positions.get(2));
            game.getS().getVehicles().get(4).updatePosition(positions.get(3));
            game.getS().getVehicles().get(5).updatePosition(positions.get(4));
            game.getS().getVehicles().get(6).updatePosition(positions.get(5));
            for (int i = 7; i < game.getS().getVehicles().size(); i++)
            {
                game.getS().getVehicles().get(i).updatePosition(positions.get(i));  
            }
        }else if(id==8){
            game.getS().getVehicles().get(1).updatePosition(positions.get(0));  
            game.getS().getVehicles().get(2).updatePosition(positions.get(1));
            game.getS().getVehicles().get(3).updatePosition(positions.get(2));
            game.getS().getVehicles().get(4).updatePosition(positions.get(3));
            game.getS().getVehicles().get(5).updatePosition(positions.get(4));
            game.getS().getVehicles().get(6).updatePosition(positions.get(5));
            game.getS().getVehicles().get(7).updatePosition(positions.get(6));
        }
    }

    @Override
    public void updateCarPosition(ArrayList<Point2D.Float> positions)
            throws RemoteException
    {
        for (int i = 0; i < positions.size(); i++)
        {
            System.out.println(i+"= ["+positions.get(i).x+", "+positions.get(i).y+"]");         
        }
        System.out.println("My clienID is: "+clientID+" my car size is: "+ game.getS().getVehicles().size());
        updatePosition(clientID, positions);

    }

    @Override
    public void sendCurrentCarPosition(final Point2D.Float position, final int clientID) throws RemoteException
    {

        class UpdateThread implements Runnable
        {
            boolean running = true;
            public void run()
            {
                while (running)
                {
                    try
                    {
                        System.out.println(clientID+" send  his position to the server: "+position.x+", "+position.y);
                        serverSet.sendCurrentCarPosition(position, clientID);
                        Thread.sleep(20);
                    }

                    catch (RemoteException re)
                    {
                        re.printStackTrace();
                        view
                                .error("Trouble connecting to server. Please restart the server.");
                        System.exit(1);
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                        view
                                .error("Trouble connecting to server. Please restart the server.");
                        System.exit(1);
                    }
                }
            }
        }
        final UpdateThread t = new UpdateThread();
        new Thread(t).start();
    }



}

2 个答案:

答案 0 :(得分:0)

positions有0个元素或game.getS().getVehicles()<2个元素。测试这些条件并采取适当的行动。

答案 1 :(得分:0)

在你的for循环中,你几乎总是从1开始。这是正确的吗?

为了让我们找到问题,我猜你应该发布更多代码。