java.lang.NullPointerException Andriod Studio Java

时间:2020-07-18 12:58:35

标签: java android

Good Day All

我对android编码和Java编码都很陌生。开始使用Java开发井字游戏。我从实际上在Kotlin中的youtube视频中获取了参考,然后用Java将其反编译。但是每次我尝试在游戏板上放置代码时,代码都会失败(这意味着应用程序关闭)。它与placeMove函数有关,但我不知道是什么。

请相信我,在此门户网站上发布问题比编写代码要困难得多,我相信每个人都可能面临相同的问题,因此您可以了解我的状况。

YouTube video that I referred to

我所面临的错误在下面

 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.mynewticatactoe.Board.placeMove(com.example.mynewticatactoe.Cell, java.lang.String)' on a null object reference
 at com.example.mynewticatactoe.MainActivity$CellClickListener.onClick(MainActivity.java:94)
 at android.view.View.performClick(View.java:7160)
 at android.view.View.performClickInternal(View.java:7137)
 at android.view.View.access$3500(View.java:810)
 at android.view.View$PerformClick.run(View.java:27418)
 at android.os.Handler.handleCallback(Handler.java:883)
 at android.os.Handler.dispatchMessage(Handler.java:100)
 at android.os.Looper.loop(Looper.java:224)
 at android.app.ActivityThread.main(ActivityThread.java:7520)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

我创建/复制的代码如下:

  1. 名为 Cell
  2. 的Java类

    package com.example.mynewticatactoe;

    public class Cell {
        public final int i;
        public final int j;

        public int getI() {
            return this.i;
        }

        public int getJ() {
            return this.j;
        }

        public Cell(int i, int j) {
            this.i = i;
            this.j = j;
        }
    }

  1. 名为 Board
  2. 的Java类

public class Board {
        public static final String PLAYER = "O";
        public static final String COMPUTER = "X";
        @NonNull
        private String[][] board = new String[3][3];
    
        public final String[][] getBoard() {
            return this.board;
        }
    
        public void placeMove(@NonNull Cell cell, @NonNull String player) {
            this.board[cell.i][cell.j] = player;
        }
    }

  1. 主要活动Java代码:

    public class MainActivity extends AppCompatActivity {
    final ImageView[][] boardCells = new ImageView[3][3];

    public Board brdMap;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.loadBoard();

        private void mapBoardToUi() {
            for (int i = 0; i < brdMap.getBoard().length; i++) {
                for (int j = 0; j < brdMap.getBoard().length; j++) {
                    String brdCells = brdMap.getBoard()[i][j];

                    if (brdCells.equals(Board.PLAYER)) {
                        boardCells[i][j].setImageResource(R.drawable.circle);
                        boardCells[i][j].setEnabled(false);
                    } else if (brdCells.equals(Board.COMPUTER)) {
                        boardCells[i][j].setImageResource(R.drawable.cross);
                        boardCells[i][j].setEnabled(false);
                    } else {
                        boardCells[i][j].setImageResource(0);
                        boardCells[i][j].setEnabled(true);
                    }
                }
            }
    }

    public void loadBoard() {
            for (int i = 0; i < boardCells.length; i++) {
                for (int j = 0; j < boardCells.length; j++) {
                    this.boardCells[i][j] = new ImageView(this);
                    ImageView brdCell = this.boardCells[i][j];
                    if (brdCell != null) {
                        GridLayout.LayoutParams pinParams = new GridLayout.LayoutParams();
                        pinParams.rowSpec = GridLayout.spec(i);
                        pinParams.columnSpec = GridLayout.spec(j);
                        pinParams.width = 250;
                        pinParams.height = 230;
                        pinParams.bottomMargin = 5;
                        pinParams.topMargin = 5;
                        pinParams.leftMargin = 5;
                        pinParams.rightMargin = 5;
                        brdCell.setLayoutParams(pinParams);
                        brdCell.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary));
                        brdCell.setOnClickListener(new CellClickListener(i, j));
                    }
                    GridLayout layout = findViewById(R.id.layout_board);
                    layout.addView(brdCell);
                }
            }
    }

    public class CellClickListener implements OnClickListener {
            final int i;
            final int j;

            public void onClick(View v) {
                Cell cell = new Cell(this.i, this.j);
                brdMap.placeMove(cell, Board.PLAYER);
                mapBoardToUi();
            }

            public CellClickListener(int i, int j) {
                this.i = i;
                this.j = j;
            }
    }
}

我确信对于像您这样的超级编码员来说,这可能是一个很小的问题,但是对于像我这样的新手而言,这不是一个小问题。我能理解的任何建议都将受到赞赏。另外,如果您需要更多说明,请告诉我。预先感谢

1 个答案:

答案 0 :(得分:1)

在不了解您的代码的情况下乍看之下:

在MainActivity类中声明

public Board brdMap;

但是不要初始化它,所以它变为空。

尝试

public Board brdMap = new Board();