# JavaFX Button funktioniert(nicht)



## JavaUndC-Prog (21. Okt 2015)

Hallo zusammen,

ich bin gerade dabei mich in die Thematik von JavaFX einzuarbeiten.
Ich kann mir auch schon Frames anzeigen lassen. Ich habe mir dann mal einen Button erstellt und versucht diesen anzusteuern. Der Button soll mir einmal einen Text ausgeben (das klappt auch) und zusätzlich ein Label ändern. Bei letzterem bekomme ich aber recht viele Fehlermeldungen ausgegeben.

Und das ist das, was ich etwas merkwürdig finde, wenn ich auf den Button Klicke, wird mir der Text in der Konsole ausgegeben, aber der Rest, also das Label zu Ändern funktioniert nicht, wie kann das sein?

Ich stelle hier mal meinen Code zur Verfügung, falls Jemand eine Lösung weiß würde ich mich sehr freuen!

Ich benutze Eclipse und den Scene Builder.

MyController Klasse:


Spoiler: MyController





```
package controller;

import java.net.URL;
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.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;


public class MyController implements Initializable{

    @FXML
    public static Button btnNewNote;
    @FXML
    public static Label lb001;
    @FXML
    public static CheckBox chk001;
    @FXML
    public static CheckBox chk002;
    @FXML
    public static TextField tb001;
    @FXML
    public static TextField tb002;

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub

    }

    public void newNote(ActionEvent event){
        System.out.println("Button: Neue Notiz");
        System.out.println(tb001.getText());
        lb001.setText(tb001.getText());
    }
}
```




Main Klasse:


Spoiler: Main





```
package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;



public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("/application/FrameGUI.fxml"));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.setTitle("Notizen");
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);

    }
}
```




Frame FXML:


Spoiler: FXML





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

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

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="313.0" prefWidth="654.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.MyController">
   <children>
      <Button fx:id="btnNewNote" layoutX="14.0" layoutY="14.0" mnemonicParsing="false" onAction="#newNote" text="+" />
      <CheckBox fx:id="chk001" layoutX="66.0" layoutY="71.0" mnemonicParsing="false" />
      <TextField fx:id="tb001" layoutX="87.0" layoutY="67.0" prefHeight="25.0" prefWidth="558.0" />
      <Label fx:id="lb001" layoutX="52.0" layoutY="18.0" text="Neue Notiz hinzufügen" />
      <Line endX="-320.0" layoutX="320.0" layoutY="51.0" startX="333.0" strokeWidth="3.0" />
      <TextField fx:id="tb002" layoutX="87.0" layoutY="99.0" prefHeight="25.0" prefWidth="558.0" />
      <CheckBox fx:id="chk002" layoutX="66.0" layoutY="103.0" mnemonicParsing="false" />
   </children>
</Pane>
```




Fehlermeldungen:


Spoiler: Fehler



Exception in thread "JavaFX Application Thread" Button: Neue Notiz
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Node.fireEvent(Unknown Source)
    at javafx.scene.control.Button.fire(Unknown Source)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Scene$MouseHandler.process(Unknown Source)
    at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
    at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(Unknown Source)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.notifyMouse(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$149(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.Trampoline.invoke(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
    ... 49 more
Caused by: java.lang.NullPointerException
    at controller.MyController.newNote(MyController.java:43)
    ... 58 more


----------



## Tom299 (21. Okt 2015)

Versuchs mit Platform.runLater() { ... Label setzen ... }


----------



## JavaUndC-Prog (21. Okt 2015)

Okay danke, muss die Methode in die Klasse MyController gepackt werden?


----------



## JavaUndC-Prog (21. Okt 2015)

Ich habe die Lösung:

Es lag an einem Static!

Die Definition der Controlls, Button und Co. dürfen nicht static sein!


----------

