0

I can't set the style of a button simply within an if-then-statement or a method.

It compiles well but gives an error when running the application. The idea is to color a button when the parameter "setnrColor" is equal to 7. The button is assigned with the @FXML label. The logic works well when I do not use the setStyle / setTextFill methods referring to the button.

When running, I get the following error: Exception in thread "JavaFX Application Thread" java.lang.NullPointerException. This occurs when the "setstyle methode is being called.

A simple explanation saying it is not possible would make sense. With all my searches so far I didn't got this answer though. Any help is appreciated.

public class FXMLDocumentController implements Initializable {

@FXML private Label label; 
@FXML private Button btn;
@FXML private TextField text;
private int NrColor; 

public void setButtonColor (int setnrColor){

boolean bln1;

NrColor = setnrColor;

if(NrColor==7){ //Expression is of type boolean
System.out.println("SAME NUMBER: SET COLOR BUTTON TO BLACK");
//btn.setStyle("-fx-base:black;"); //THIS LINE DOESNT WORK
bln1 = true; 
//btn.getStyleClass().remove("armed");
}  
else {
System.out.println("DIFFERENT NUMBER: SET COLOR BUTTON TO RED");
//btn.setStyle("-fx-base:red;"); //DOESNT WORK
bln1 = false; 
trigger();

}

System.out.println("OUTPUT =" + bln1);

}


java.lang.NullPointerException:
at test_issue.FXMLDocumentController.trigger(FXMLDocumentController.java:64)
at test_issue.FXMLDocumentController.setButtonColor(FXMLDocumentController.java:53)
at test_issue.FlashingLight.lambda$start$1(FlashingLight.java:25)
at com.sun.scenario.animation.shared.TimelineClipCore.visitKeyFrame(TimelineClipCore.java:239)
at com.sun.scenario.animation.shared.TimelineClipCore.playTo(TimelineClipCore.java:180)
at javafx.animation.Timeline.impl_playTo(Timeline.java:176)
at javafx.animation.AnimationAccessorImpl.playTo(AnimationAccessorImpl.java:39)
at com.sun.scenario.animation.shared.FiniteClipEnvelope.timePulse(FiniteClipEnvelope.java:124)
at javafx.animation.Animation.impl_timePulse(Animation.java:1102)
at javafx.animation.Animation$1.lambda$timePulse$25(Animation.java:186)
at java.security.AccessController.doPrivileged(Native Method)
at javafx.animation.Animation$1.timePulse(Animation.java:185)
at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:344)
at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:267)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:506)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$404(QuantumToolkit.java:319)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)

/////////////////////////////////////////////
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test_issue.FXMLDocumentController">
<children>
<Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
<Button fx:id="btn" layoutX="104.0" layoutY="72.0" mnemonicParsing="false" prefHeight="56.0" prefWidth="136.0" text="Button" />
<TextField fx:id="text" layoutX="41.0" layoutY="14.0" promptText="HALLO" />
</children>
</AnchorPane>
Bram
  • 13
  • 7
  • please show the stack trace for the nullpointerexception – JohnnyAW Nov 10 '16 at 13:35
  • There are 2 possibilities: a) The ` – fabian Nov 10 '16 at 13:40
  • Thanks for the anwers. I checked the fx:id the fxml file and it was named "btn". But the problem exists. – Bram Nov 10 '16 at 13:59
  • [Edit] your question to include the full stack trace and the FXML file. – James_D Nov 10 '16 at 14:17
  • @fabian, I am looking at possibility b. The FXMLDocumentController is set in the fxml file so it should know this is the right controller to use. But Iam creating an instance of FXMLDocumentController in another class because i cant use a static method call of "setButtonColor" because the btn object is not static. Thats why i have created an instance of FXMLDocumentController. – Bram Nov 10 '16 at 14:24
  • @Bram I updated my answer – JohnnyAW Nov 10 '16 at 14:30

1 Answers1

0

you shouldn't create objects of your controller class, you need to extract if from the loader:

FXMLLoader loader = new FXMLLoader(getClass().getResource("/main.fxml"));
AnchorPane root = loader.load();

FXMLDocumentController controller = loader.getController();
controller.setButtonColor(...);
JohnnyAW
  • 2,866
  • 1
  • 16
  • 27
  • Iam really glad you edited your last comment. That did the trick. Really happy, thx man :) – Bram Nov 10 '16 at 15:08
  • Is there a way to call this controller.setButtonColor(..); method based on a trigger from another class? Because now it gets called only once by the start of the application. Basically the method needs to be triggered every few seconds from another class. – Bram Nov 10 '16 at 18:02
  • @Bram you can pass the controller as an parameter to the class where you need it. – JohnnyAW Nov 10 '16 at 23:28