线程" main"中的例外情况在Java中访问变量时出现java.lang.NullPointerException

时间:2015-04-19 02:22:08

标签: java

package model;

import model.interfaces.DicePair;
import model.interfaces.Player;


public class SimplePlayer implements Player{

private String playerName;
private int points;
private String playerId;
private int bet;
DicePair dicePair;


public SimplePlayer(String playerId,String playerName,int points){
    this.playerId=playerId;
    this.playerName=playerName;
    this.points=points;

}

public String getPlayerName(){
    return playerName;
}

public  void setPlayerName(String playerName){
    this.playerName=playerName;
}

public  int getPoints(){
    return points;
}

public void setPoints(int points){
    this.points=points;
}

public  String getPlayerId(){
    return playerId;
}

public  boolean placeBet(int bet){

    if(bet>getPoints()){

        return false;

    }
    else{
        this.bet=bet;

    return true;}

}

public  int getBet(){
    return bet;
}

public  DicePair getRollResult(){
    return dicePair;


}

public  void setRollResult(DicePair rollResult){
    this.dicePair=rollResult;
}

public  String toString(){
    return "Player: id="+playerId+", name="+playerName+", points="+points;
}

} //这个类有点值

    package model;

import java.util.ArrayList;
import java.util.Collection;

import model.interfaces.DicePair;
import model.interfaces.GameEngine;
import model.interfaces.GameEngineCallback;
import model.interfaces.Player;


public  class GameEngineImpl implements GameEngine {
SimplePlayer simplePlayer;
    public static int NUM_FACES = 6;
    Collection<Player> players= new ArrayList<Player>();
GameEngineCallback gameEngineCallback;
DicePair dicePair;
DicePair dicePair2;
Player player;
public void rollPlayer(Player player, int initialDelay,
        int finalDelay, int delayIncrement){


    for(int i= initialDelay; i<=finalDelay;i+=delayIncrement){
        int dice1=(int)(Math.random()*NUM_FACES+1);
        int dice2=(int)(Math.random()*NUM_FACES+1);
        DicePairImpl dicePairImpl=new DicePairImpl(dice1,dice2);
        gameEngineCallback.intermediateResult(player,dicePairImpl,this);


        try {
            Thread.sleep(initialDelay);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    int dice1=(int)(Math.random()*NUM_FACES+1);
    int dice2=(int)(Math.random()*NUM_FACES+1);
    DicePairImpl dicePairImpl=new DicePairImpl(dice1,dice2);
    gameEngineCallback.result(player,dicePairImpl,this);
    player.setRollResult(dicePairImpl);
    dicePair=dicePairImpl;
};
    public void addPlayer(Player player){
    players.add(player);

};

public boolean removePlayer(Player player){
    if(players.contains(player)==true){
        players.remove(player);
        return true;
    }
    return false;

};
public void calculateResult(){
.....
int points=simplePlayer.getPoints();// get error from here which is Exception      in thread "main" java.lang.NullPointerException
System.out.println(points);
}
}

在这个类中,对于方法calculateResult(),我需要获得玩家的积分值。我试着写int points = simplePlayer.getPoints();我继续造成错误,我丢失了,无法获得积分值。

错误如下:

  

线程“main”中的异常java.lang.NullPointerException

public class SimplePlayer implements Player{

private String playerName;
private int points;
private String playerId;
private int bet;
DicePair dicePair;


public SimplePlayer(String playerId,String playerName,int points){
    this.playerId=playerId;
    this.playerName=playerName;
    this.points=points;

}

//这是simplePlayer类

1 个答案:

答案 0 :(得分:0)

您收到错误的原因是simplePlayer为空。您可能没有使用其构造函数初始化simplePlayer。如果您希望我确认,请在声明和用法之间张贴代码。要使用构造函数,您应该使用:

simplePlayer = new SimplePlayer( 1, "Adam", 0 );

您可以在simplePlayer.getPoints()来电之前执行以下行进一步测试。

if( simplePlayer == null )
    System.out.println( "simplePlayer is null!" );

int points=simplePlayer.getPoints();

修改 在查看代码后,OP发现this.simplePlayer=simplePlayer解决了问题,使其无效。