简体   繁体   English

从另一个类java访问int []时遇到问题

[英]problem accessing an int[] from another class java

I try to run the following code, but for some reason I always get a NullPointerException when accessing field.playField from class IntelligentPlayer 我尝试运行以下代码,但是由于某些原因,从类IntelligentPlayer访问field.playField时始终会收到NullPointerException


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;
    }
}


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};
        }
    }

Edit: The Test Code is: 编辑:测试代码是:


    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);

I expected fieldCount to return an int array but got this: 我期望fieldCount返回一个int数组,但是得到了这个:

java.lang.NullPointerException
at TicTacToe.IntelligentPlayer.fieldCount(IntelligentPlayer.java:60)
at TicTacToe.IntelligentPlayer.move(IntelligentPlayer.java:24)
at TicTacToe.Field.move(Field.java:50)

Remove protected Field field; 删除protected Field field; from your IntelligentPlayer class. 从您的IntelligentPlayer类中。 This will allow the class to access the field field defined in the parent class (which is what the Field constructor is setting). 这将允许类访问在父类中定义的field字段(这是Field构造函数所设置的)。

Even better, define a setter in RealPlayer and use that instead of directly assigning the variable: 更好的是,在RealPlayer定义一个setter并使用它而不是直接分配变量:

    public void setField(Field field) {
        this.field = field;
    }
    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};
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM