проблема доступа к int [] из другого класса Java - PullRequest
1 голос
/ 18 июня 2019

Я пытаюсь запустить следующий код, но по какой-то причине я всегда получаю NullPointerException при доступе к field.playField из класса IntelligentPlayer


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

Редактировать: Тестовый код:


    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 вернет массив 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)

1 Ответ

0 голосов
/ 18 июня 2019

Удалите protected Field field; из вашего IntelligentPlayer класса. Это позволит классу получить доступ к полю field, определенному в родительском классе (это то, что устанавливает конструктор Field).

Еще лучше, определить сеттер в RealPlayer и использовать его вместо непосредственного присвоения переменной:

    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};
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...