This is part of my fxml code
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.Hyperlink?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ToggleButton?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.Rectangle?>
<?import javafx.scene.text.Font?>
<AnchorPane id="AnchorPane" prefHeight="467.0" prefWidth="1024.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="teacherattendencesystem.FXMLDocumentController">
<children>
<Label fx:id="header" layoutX="217.0" layoutY="14.0" minHeight="16" minWidth="69" text="UOS Teacher Attendance System">
<font>
<Font name="System Bold" size="36.0" />
</font>
</Label>
<Button fx:id="j1" layoutX="22.0" layoutY="166.0" mnemonicParsing="false" onAction="#marked" prefHeight="30.0" prefWidth="141.0" text="Eisha Tir Razia 1">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Button>
This is part of my controller class
import java.net.URL;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
public class FXMLDocumentController implements Initializable {
@FXML
private Label dayHeading;
private Button j1,j2,j3,j4,j5,j6,j7,j8,j9;
private boolean isSelected = false;
List<Button> room = new ArrayList();
@FXML
private void loadButtons(){
j1.setText("Room 1");
}
private String getDay(){
DayOfWeek dayOfWeek = DayOfWeek.from(LocalDate.now());
return (String) dayOfWeek.name();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
dayHeading.setText(getDay());
loadButtons();
}
}
But when I run the code I basically get a null pointer exception i.e j1 has nothing in it. I've been trying to find the reason for it for almost an hour now but I have no idea what I'm doing wrong.
- Assign fx:id to component
- declare component in controller class
- use component
Now, while I am able to manipulate the Label I am unable to manipulate the Button. Why is that?