0

public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button[][] buttons = new Button[3][3]; private boolean player1Turn = true;

private int roundCount;

private int player1Points;
private int player2Points;

private TextView text1;
private TextView text2;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView text1 = findViewById(R.id.t1);
    TextView text2 = findViewById(R.id.t2);
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            String buttonID = "button_" + i + j;
            int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
            buttons[i][j] = findViewById(resID);
            buttons[i][j].setOnClickListener(this);


        }
    }
    Button reset = findViewById(R.id.reset);
    reset.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
}

@Override
public void onClick(View v) {
    if (!((Button) v).getText().toString().equals("")) {
        return;
    }
    if (player1Turn) {
        ((Button) v).setText("X");
    } else {
        ((Button) v).setText("O");
    }
    roundCount++;
    if (checkForWin()) {
        if (player1Turn) {
            player1Wins();
        } else {
            player2Wins();

        }
    } else if (roundCount == 9) {
        draw();
    } else {
        player1Turn = !player1Turn;
    }


}

private boolean checkForWin() {
    String[][] field = new String[3][3];
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            field[i][j] = buttons[i][j].getText().toString();

        }
    }
    for (int i = 0; i < 3; i++) {
        if (field[i][0].equals(field[i][1])
                && field[i][0].equals(field[i][2])
                && !field[i][0].equals("")) {
            return true;
        }
    }
    for (int i = 0; i < 3; i++) {
        if (field[0][i].equals(field[1][i])
                && field[0][i].equals(field[2][i])
                && !field[0][i].equals("")) {
            return true;
        }
    }
    if (field[0][0].equals(field[1][1])
            && field[0][0].equals(field[2][2])
            && !field[0][0].equals("")) {
        return true;
    }
    if (field[0][2].equals(field[1][1])
            && field[0][2].equals(field[2][0])
            && !field[0][2].equals("")) {
        return true;
    }
    return false;

}

private void player1Wins() {
    player1Points++;
    Toast.makeText(this, "Player 1 Wins!!", Toast.LENGTH_SHORT).show();
    updatePointsText();
    resetBoard();
}

private void player2Wins() {
    player2Points++;
    Toast.makeText(this, "Player 2 Wins!!", Toast.LENGTH_SHORT).show();
    updatePointsText();
    resetBoard();
}

private void draw() {
    Toast.makeText(this, "Draw!!", Toast.LENGTH_SHORT).show();
    resetBoard();
}
@SuppressLint("DefaultLocale")
private void updatePointsText(){
    text1.setText("Player 1: "+player1Points);
    text2.setText("Player 2: "+player2Points);
}
private void resetBoard(){
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            buttons[i][j].setText("");
        }
    }
    roundCount =0;
    player1Turn =true;
}

}

--------- beginning of crash E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.tictaktoe, PID: 3016 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.tictaktoe.MainActivity.updatePointsText(MainActivity.java:133) at com.example.tictaktoe.MainActivity.player1Wins(MainActivity.java:116) at com.example.tictaktoe.MainActivity.onClick(MainActivity.java:63) at android.view.View.performClick(View.java:4780) at android.view.View$PerformClick.run(View.java:19866) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

1 Answers1

0

Can you replace the text1 and text2 with class variables rather than local variables in onCreate. text1 and text2 are already given as class variables at the beginning of the class. But the values are assigned to local variables.

As below:

@Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      text1 = findViewById(R.id.t1);
      text2 = findViewById(R.id.t2);
    ...
 }
ACR
  • 317
  • 1
  • 8