Обращение к элементу Button - Java
Формулировка задачи:
При нажатии на menuItem нужно чтобы на всех Button стирался текст.
Почему в 17 строке выдает ошибку java.lang.NullPointerException?
Листинг программы
- public class MainController {
- @FXML Button cell_1;
- @FXML Button cell_2;
- @FXML Button cell_3;
- @FXML Button cell_4;
- @FXML Button cell_5;
- @FXML Button cell_6;
- @FXML Button cell_7;
- @FXML Button cell_8;
- @FXML Button cell_9;
- ArrayList<Button> listOfButtons = new ArrayList<Button>(Arrays.asList(cell_1, cell_2, cell_3, cell_4, cell_5, cell_6, cell_7, cell_8, cell_9));
- @FXML
- private void startNewGame() {
- for(int i = 0; i < 9; i++) {
- listOfButtons.get(i).setText("");
- }
- }
- }
Решение задачи: «Обращение к элементу Button»
textual
Листинг программы
- Button[] cell;
- public void initButtons() {
- cell = new Button[9];
- for (int i = 0; i < cell.length; i++) {
- cell[i] = new Button(i + "");
- }
- }
- @FXML
- private void startNewGame() {
- for (int i = 0; i < cell.length; i++) {
- cell[i].setText("");
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д