首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从另一个类访问int[]时出现问题

从另一个类访问int[]时出现问题
EN

Stack Overflow用户
提问于 2019-06-19 03:02:17
回答 1查看 19关注 0票数 1

我尝试运行以下代码,但由于某些原因,从类IntelligentPlayer访问field.playField时总是得到一个NullPointerException

代码语言:javascript
复制
public class IntelligentPlayer extends RealPlayer {
    private int[][] winning = new int[][]{{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6}};
    protected Field field;
    protected int self;
    public int move(int move) {
        for (int[] possibility:winning) {
            return fieldCount(possibility, 3)[1];
        }
    }
    private int[] fieldCount(int[] test, int len) {
        System.out.println("" + test[0] + test[1] + test[2]);
        System.out.println(field.playField[2]);
            int[] temp = new int[]{0, 0, 0};
        for (int i = 0; i < len; i++) {
            if (field.playField[test[i]]==self) {
                temp[0]+=1;
            }else if (field.playField[test[i]]==(self*-1)) {
                temp[1]+=1;
            } else {
                temp[2]+=1;
            }
        }
        return temp;
    }
}
代码语言:javascript
复制
public class Field {
        int[][] winning = new int[][]{{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6}};
        public int[] playField = new int[9];
        protected char[] symbols = new char[]{'O', ' ', 'X'};
        private int turn;
        private RealPlayer player1;
        private RealPlayer player2;

        public Field(RealPlayer pl1, RealPlayer pl2) {
            this.player1 = pl1;
            this.player2 = pl2;
            this.player1.field = this;
            this.player2.field = this;
            this.player1.self = -1;
            this.player2.self = 1;
            this.playField = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0};
        }
    }

编辑:测试代码为:

代码语言:javascript
复制
    Field f = new Field(new RealPlayer(), new IntelligentPlayer(), -1);
    f.move(2);
    System.out.println(f.toString());
    f.move(2);
    System.out.println(f.toString());
    f.move(0);

我期望fieldCount返回一个整型数组,但得到的结果是:

代码语言:javascript
复制
java.lang.NullPointerException
at TicTacToe.IntelligentPlayer.fieldCount(IntelligentPlayer.java:60)
at TicTacToe.IntelligentPlayer.move(IntelligentPlayer.java:24)
at TicTacToe.Field.move(Field.java:50)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-19 03:30:53

IntelligentPlayer类中删除protected Field field;。这将允许该类访问父类中定义的field字段(这是Field构造函数设置的内容)。

更好的是,在RealPlayer中定义一个设置器并使用它,而不是直接为变量赋值:

代码语言:javascript
复制
    public void setField(Field field) {
        this.field = field;
    }
代码语言:javascript
复制
    public Field(RealPlayer pl1, RealPlayer pl2) {
        this.player1 = pl1;
        this.player2 = pl2;
        this.player1.setField(this);
        this.player2.setField(this);
        this.player1.self = -1;
        this.player2.self = 1;
        this.playField = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0};
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56655487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档