[JavaFX] Problem z edycją obiektu typu Label

0

Cześć,
Mam problem ze zmianą napisu na drugim obiekcie typu Label. Zmieniając napis na pierwszym obiekcie wyświetla mi się wszystko poprawnie, jednak jeżeli dodam drugi obiekt i próbuję mu zmienić tekst, to kompilator rzuca mi błędem:

IDE - Intelij / ScenBuilder

Java - 10.0.2

Błąd:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
    at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8865)
    at javafx.controls/javafx.scene.control.Button.fire(Button.java:200)
    at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
    at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3876)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1300(Scene.java:3604)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1874)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2613)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
    at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1784)
    ... 47 more
Caused by: java.lang.NullPointerException
    at TempController.settingsButton(TempController.java:44)
    ... 58 more

Kod programu:

mport javafx.fxml.FXML;
 
import javafx.scene.control.Label;
 
/**
 * Created $(DATE)
 */
public class TempController {
 
@FXML
private Label nameOfTab;
@FXML
private Label test;
 
@FXML
public void pulpitButton()
{
    nameOfTab.setText("Pulpit");
    test.setText("ok");
}
 
    @FXML
    public void incomeButton()
    {
        nameOfTab.setText("Wpływy");
    }
 
    @FXML
    public void expenditiuresButton()
    {
        nameOfTab.setText("Wydatki");
    }
 
    @FXML
    public void boudgetButton()
    {
        nameOfTab.setText("Budżet");
    }
 
    @FXML
    public void settingsButton()
    {
        nameOfTab.setText("Ustawienia");
    }
 
    @FXML
    public void exitButton()
    {
        nameOfTab.setText("Wyjście");
    }
 
}
0

Dodaję kod reszty programu.

fxml

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

<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.effect.Blend?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="515.0" prefWidth="859.0" styleClass="style.css" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="TempController">
    <effect>
        <Blend mode="SRC_ATOP" />
    </effect>
    <left>
        <VBox alignment="TOP_CENTER" prefHeight="483.0" prefWidth="152.0" style="-fx-padding: 10; -fx-background-color: #9598E8;" BorderPane.alignment="CENTER">
            <cursor>
                <Cursor fx:constant="DEFAULT" />
            </cursor>
            <children>
                <Button mnemonicParsing="false" onAction="#pulpitButton" prefHeight="14.0" prefWidth="132.0" text="Pulpit">
                    <font>
                        <Font name="Symbol" size="14.0" />
                    </font>
                    <cursor>
                        <Cursor fx:constant="HAND" />
                    </cursor>
                    <effect>
                        <DropShadow color="#0c73ff" />
                    </effect>
                </Button>
                <Button layoutX="20.0" layoutY="20.0" mnemonicParsing="false" onAction="#incomeButton" prefHeight="23.0" prefWidth="142.0" text="Wpływy">
                    <font>
                        <Font name="Symbol" size="14.0" />
                    </font>
                    <cursor>
                        <Cursor fx:constant="HAND" />
                    </cursor>
                    <effect>
                        <DropShadow color="#0c73ff" />
                    </effect>
                </Button>
                <Button layoutX="20.0" layoutY="50.0" mnemonicParsing="false" onAction="#expenditiuresButton" prefHeight="23.0" prefWidth="142.0" text="Wydatki">
                    <font>
                        <Font name="Symbol" size="14.0" />
                    </font>
                    <cursor>
                        <Cursor fx:constant="HAND" />
                    </cursor>
                    <effect>
                        <DropShadow color="#0c73ff" />
                    </effect>
                </Button>
                <Button layoutX="20.0" layoutY="80.0" mnemonicParsing="false" onAction="#boudgetButton" prefHeight="23.0" prefWidth="142.0" text="Budżet">
                    <font>
                        <Font name="Symbol" size="14.0" />
                    </font>
                    <cursor>
                        <Cursor fx:constant="HAND" />
                    </cursor>
                    <effect>
                        <DropShadow color="#0c73ff" />
                    </effect>
                </Button>
                <Button layoutX="20.0" layoutY="110.0" mnemonicParsing="false" onAction="#settingsButton" prefHeight="23.0" prefWidth="142.0" text="Ustawienia">
                    <font>
                        <Font name="Symbol" size="14.0" />
                    </font>
                    <cursor>
                        <Cursor fx:constant="HAND" />
                    </cursor>
                    <effect>
                        <DropShadow color="#0c73ff" />
                    </effect>
                </Button>
                <Button layoutX="20.0" layoutY="140.0" mnemonicParsing="false" onAction="#exitButton" prefHeight="23.0" prefWidth="142.0" text="Wyjście">
                    <font>
                        <Font name="Symbol" size="14.0" />
                    </font>
                    <cursor>
                        <Cursor fx:constant="HAND" />
                    </cursor>
                    <effect>
                        <DropShadow color="#0c73ff" />
                    </effect>
                </Button>
            </children>
        </VBox>
    </left>
    <top>
        <Pane prefHeight="60.0" prefWidth="814.0" style="-fx-background-color: #0C73FF;" BorderPane.alignment="CENTER">
            <children>
                <Label layoutX="335.0" layoutY="11.0" opacity="0.74" prefHeight="39.0" prefWidth="189.0" style="-fx-: 0;" text="EconMaker 3" textAlignment="CENTER" textFill="WHITE">
                    <font>
                        <Font name="Wingdings 2" size="33.0" />
                    </font>
                </Label>
                <Label fx:id="nameOfTab" layoutX="746.0" layoutY="42.0" text="Pulpit" textFill="WHITE" />
            </children>
        </Pane>
    </top>
    <center>
        <TabPane prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER">
            <tabs>
                <Tab fx:id="test">
                    <content>
                        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
                    </content>
                </Tab>
            </tabs>
        </TabPane>
    </center>
</BorderPane>

Main

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

/**
 * Created $(DATE)
 */
public class Main extends Application
{

    @Override
    public void start(Stage primaryStage) throws Exception
    {
        FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("Template.fxml"));
        Parent layout = fxmlLoader.load();

        Scene scene = new Scene(layout);
        primaryStage.setScene(scene);
        primaryStage.setTitle("EconMaker 3");
        primaryStage.show();
    }

}

1

W kodzie element test to Label:

@FXML
private Label test;

a na layoucie Tab

 <Tab fx:id="test">

tak to się raczej nie będzie wstrzykiwać.

Jeżeli zaczynasz od layoutu to polecam generować kklasę kontrolera z poziomu SceneBuildera.

0

Ok, rozumiem swój błąd,
Dziękuje za poświęcony czas. ;)

1 użytkowników online, w tym zalogowanych: 0, gości: 1