日期:2014-05-18 浏览次数:20600 次
package rungame;
public class RouteState
{
public int positionX=0;
public int positionY=0;
public void set(int x,int y){
positionX = x;
positionY = y;
}
public int getX(){
return positionX;
}
public int getY(){
return positionY;
}
}
package rungame;
import java.util.Vector;
public class PlayerRoute {
public static PlayerRoute instance = null;
//private RouteState rs = null;
private Vector<RouteState> playerroutestate = null;
private PlayerRoute(){
if(playerroutestate==null){
playerroutestate = new Vector<RouteState>();
}
}
public static PlayerRoute getInstance(){
if(instance ==null){
instance = new PlayerRoute();
}
return instance;
}
public void addRoute(RouteState rs){
if(rs!=null){
playerroutestate.addElement(rs);
}//出错行
}
public void delLastRoute(){
playerroutestate.remove(playerroutestate.size());
}
public Vector<RouteState> returnLastRoute(){
delLastRoute();
return playerroutestate;
}
public Vector<RouteState> getPlayerRoute(){
return playerroutestate;
}
}
import rungame.PlayerRoute;
import rungame.RouteState;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
RouteState rs = new RouteState();
rs.set(2, 1);
//System.out.print(rs.getX()+"\n");
PlayerRoute pr= PlayerRoute.getInstance();
pr.addRoute(rs);//出错行
rs=(RouteState)pr.getPlayerRoute().elementAt(1);
int i =rs.getX();
System.out.print(i);
}
}
Exception in thread "main" java.lang.NullPointerException
at rungame.PlayerRoute.addRoute(PlayerRoute.java:25)
at test.main(test.java:16)