线程“main”中的异常java.lang.NullPointerException为什么?

时间:2014-01-10 19:24:20

标签: java nullpointerexception

错误是:

Exception in thread "main" java.lang.NullPointerException

我想为我的大学制作一个tic tac toe控制台游戏。这是下面的代码

主程序:

import acm.program.*;
public class askisi1d extends Program {
    public void run(){
        tictac player[];
        player=new tictac[2];
        player[0]=new tictac("baggelis");
        player[1]=new tictac("Giorgaras");
        player[0].setMove("1 2");
        println(tictac.drawTable());

    }

}

这是类代码:

import java.util.*;
import acm.program.*;
public class tictac{
    private String name;
    int [][]table=new int [][]{
    {8,1,6},
    {3,5,7},
    {4,9,2}
    };
    int activePlayer;
    boolean [][]takenSquare= new boolean [2][2];
    int [][]playerTable; //gia tin niki
    static tictac[] player;
    private int row;  //<---
    private  int col;  //""
    public tictac(String name){
        this.name=name;
        playerTable=new int[2][2];
        activePlayer=0;
    }
    public boolean isValidMove(int row,int col){
        return (row>0&&row<4&&col>0&&col<4&&takenSquare[row-1][col-1]==false);
    }//isValidmove
    public void setMove (String move){
        StringTokenizer tokenizer=new StringTokenizer(move);
        this.row= Integer.parseInt(tokenizer.nextToken());
        this.col= Integer.parseInt(tokenizer.nextToken());
        if (isValidMove(row-1,col-1)){
            player[activePlayer].takenSquare[row-1][col-1]=true;
            player[activePlayer].playerTable[row-1][col-1]=table[row-1][col-1];
        }
        activePlayer=1-activePlayer;
    }
    public static String drawTable(){

        String a="";
        a+=(drawSquare(0,0)+"|");
        a+=(drawSquare(0,1)+"|");
        a+=(drawSquare(0,2)+'\n');
        a+=("-----------")+'\n';
        a+=drawSquare(1,0)+"|";
        a+=drawSquare(1,1)+"|";
        a+=(drawSquare(1,2)+'\n');
        a+=("-----------")+'\n';
        a+=drawSquare(2,0)+"|";
        a+=drawSquare(2,1)+"|";
        a+=(drawSquare(2,2));
        return"a";
    }

    public static String drawSquare(int x,int y){
        if (player[0].isTaken(x,y)) {
            return "O";
        }   else if (player[1].isTaken(x,y)) return "X";
        else return "   ";  

    }
    public boolean isTaken(int x,int y) {
        return takenSquare[x][y];
    }
}

2 个答案:

答案 0 :(得分:2)

有几个问题:

您已在tictac课程中定义了静态播放数组。 run()定义了它的本地副本,并且只要您调用drawTable(),它就会取消未初始化的静态数组,忽略run()中的内容。

您可以通过初始化来正确使用静态变量。 快速和脏的检查是在你的tictac类中编写main方法并注释掉该数组的本地版本。

类似的东西:

    public static void main(String[] args){

                    //TicTac player[];
                    player=new TicTac[2];
                    player[0]=new TicTac("baggelis");
                    player[1]=new TicTac("Giorgaras");
                    player[0].setMove("1 2");
                    System.out.println(TicTac.drawTable());


        }

正如评论中所指出的那样,现在你会遇到ArrayIndexOutOfBounds异常因此进行这些修改,修改后的代码如下:

import java.util.StringTokenizer;

public class TicTac{
    private String name;
    int [][]table=new int [][]{
    {8,1,6},
    {3,5,7},
    {4,9,2}
    };
    int activePlayer;
    boolean [][]takenSquare= new boolean [3][3];
    int [][]playerTable; //gia tin niki
    static TicTac[] player;
    private int row;  //<---
    private  int col;  //""
    public TicTac(String name){
        this.name=name;
        playerTable=new int[2][2];
        activePlayer=0;
    }
    public boolean isValidMove(int row,int col){
        return (row>0&&row<4&&col>0&&col<4&&takenSquare[row-1][col-1]==false);
    }//isValidmove
    public void setMove (String move){
        StringTokenizer tokenizer=new StringTokenizer(move);
        this.row= Integer.parseInt(tokenizer.nextToken());
        this.col= Integer.parseInt(tokenizer.nextToken());
        if (isValidMove(row-1,col-1)){
            player[activePlayer].takenSquare[row-1][col-1]=true;
            player[activePlayer].playerTable[row-1][col-1]=table[row-1][col-1];
        }
        activePlayer=1-activePlayer;
    }
    public static String drawTable(){

        String a="";
        a+=(drawSquare(0,0)+"|");
        a+=(drawSquare(0,1)+"|");
        a+=(drawSquare(0,2)+'\n');
        a+=("-----------")+'\n';
        a+=drawSquare(1,0)+"|";
        a+=drawSquare(1,1)+"|";
        a+=(drawSquare(1,2)+'\n');
        a+=("-----------")+'\n';
        a+=drawSquare(2,0)+"|";
        a+=drawSquare(2,1)+"|";
        a+=(drawSquare(2,2));
        return"a";
    }

    public static String drawSquare(int x,int y){
        if (player[0].isTaken(x,y)) {
            return "O";
        }   else if (player[1].isTaken(x,y)) return "X";
        else return "   ";  

    }
    public boolean isTaken(int x,int y) {
        return takenSquare[x][y];
    }

    public static void main(String[] args){

                //TicTac player[];
                player=new TicTac[2];
                player[0]=new TicTac("baggelis");
                player[1]=new TicTac("Giorgaras");
                player[0].setMove("1 2");
                System.out.println(TicTac.drawTable());


    }


}

答案 1 :(得分:1)

这应该使您的代码有效。至少它不会在drawSquare()

中抛出NullPointerException
public class tictac {
... //rest of the code

    static tictac[] player = { 
        new tictac( "PlayerName1" ),
        new tictac( "PlayerName2" ),
    };

... //more code

}
编辑:好的,也许我没有想象的那么简单。这是我希望你运行的类tictac的完整代码

import java.util.*;
import acm.program.*;
public class tictac{
    private String name;
    int [][]table=new int [][]{
    {8,1,6},
    {3,5,7},
    {4,9,2}
    };
    int activePlayer;
    boolean [][]takenSquare= new boolean [2][2];
    int [][]playerTable; //gia tin niki
    static tictac[] player = { 
        new tictac( "PlayerName1" ),
        new tictac( "PlayerName2" ),
    };
    private int row;  //<---
    private  int col;  //""
    public tictac(String name){
        this.name=name;
        playerTable=new int[2][2];
        activePlayer=0;
    }
    public boolean isValidMove(int row,int col){
        return (row>0&&row<4&&col>0&&col<4&&takenSquare[row-1][col-1]==false);
    }//isValidmove
    public void setMove (String move){
        StringTokenizer tokenizer=new StringTokenizer(move);
        this.row= Integer.parseInt(tokenizer.nextToken());
        this.col= Integer.parseInt(tokenizer.nextToken());
        if (isValidMove(row-1,col-1)){
            player[activePlayer].takenSquare[row-1][col-1]=true;
            player[activePlayer].playerTable[row-1][col-1]=table[row-1][col-1];
        }
        activePlayer=1-activePlayer;
    }
    public static String drawTable(){

        String a="";
        a+=(drawSquare(0,0)+"|");
        a+=(drawSquare(0,1)+"|");
        a+=(drawSquare(0,2)+'\n');
        a+=("-----------")+'\n';
        a+=drawSquare(1,0)+"|";
        a+=drawSquare(1,1)+"|";
        a+=(drawSquare(1,2)+'\n');
        a+=("-----------")+'\n';
        a+=drawSquare(2,0)+"|";
        a+=drawSquare(2,1)+"|";
        a+=(drawSquare(2,2));
        return"a";
    }

    public static String drawSquare(int x,int y){
        if (player[0].isTaken(x,y)) {
            return "O";
        }   else if (player[1].isTaken(x,y)) return "X";
        else return "   ";  

    }
    public boolean isTaken(int x,int y) {
        return takenSquare[x][y];
    }
}