-1

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?

Talha
  • 55
  • 1
  • 7
  • Make sure you imported `JavaFX` `Button`. – SedJ601 Mar 01 '18 at 19:12
  • 1
    Where are you calling `renameButtons`? It's a private method not annotated with fxml, so this must be a method of the controller class you did not post. The code seems correct though, which means you probably do something like this: `someVariable = FXMLLoader.load(someUrl); FXMLDocumentController controller = new FXMLDocumentController(); controller.methodThatCallsRenameButtons();` – fabian Mar 01 '18 at 19:21
  • Fix the example out so that it constitutes a [MCVE]. As fabian says, you are probably not calling this method on the actual controller. – James_D Mar 01 '18 at 19:34
  • @fabian I edited & posted the entire code I am using. I am calling the **renameButtons** method inside the initialize method just like I am calling the **getDay()** method. – Talha Mar 01 '18 at 19:52
  • Shouldn't you add the `@FXML` annotation above your button declaration as well? i.e. `@FXML private Button j1;` – TM00 Mar 01 '18 at 20:06
  • oh, that worked @TM00 . I see I have to add the annotation to each declaration individually. – Talha Mar 01 '18 at 20:21
  • Yup... I posted it in an answer. Please accept it if you are happy with it :) – TM00 Mar 01 '18 at 20:23

1 Answers1

1

Just add the @FXML annotation to the button:

@FXML 
private Button j1;

Each variable that is injected by the FXML loader must be indicated as such with the annotation.

TM00
  • 1,330
  • 1
  • 14
  • 26