index ot of bounds exception
game.getS().getVehicles().get(1).updatePosition(positions.get(0));
we are making a client - server racing game and we got this exception in this exact line what can we do or what can we change? here i will show you the all code:
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();
}
}
In your for-loops you almost always start from 1. Is that correct?
To let us find the problem, you should post more code, I guess.
Either positions
has 0 elements or game.getS().getVehicles()
has <2
elements. Test for those conditions and act appropriately.
精彩评论