Я пытаюсь настроить музыкальный проигрыватель, но прежде чем начать, я просто хотел поэкспериментировать с некоторыми вещами, которые я могу сделать с JavaFX.Тем не менее, я столкнулся с NullPointerException при попытке обновить текст в метке.
Я уже пытался изменить модификаторы доступа и изменить названия меток безрезультатно.
Main.java
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception{
Font.loadFont(Main.class.getResource("/fonts/Inter-SemiBold.otf").toExternalForm(), 12);
Font.loadFont(Main.class.getResource("/fonts/Inter-ExtraBold.otf").toExternalForm(), 12);
Font.loadFont(Main.class.getResource("/fonts/Inter-SemiBoldItalic.otf").toExternalForm(), 12);
Font.loadFont(Main.class.getResource("/fonts/Inter-Bold.otf").toExternalForm(), 12);
Parent root = FXMLLoader.load(getClass().getResource("resources/fxml/Coffea.fxml"));
primaryStage.setTitle("Coffea");
primaryStage.setScene(new Scene(root, 960, 540));
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("resources/img/coffee-cup.png")));
primaryStage.setResizable(false);
primaryStage.show();
}
}
Controller.java
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
public class Controller {
@FXML
private Button rewind;
@FXML
private Button play;
@FXML
private Button forward;
@FXML
private Label songName;
@FXML
private Label artistName;
@FXML
void pause(ActionEvent event) {
artistName.setText("Hello World");
}
@FXML
void playlists(ActionEvent event) {
}
@FXML
void previous(ActionEvent event) {
}
@FXML
void skip(ActionEvent event) {
}
@FXML
void songs(ActionEvent event) {
}
}
}
Coffea.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Hyperlink?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="540.0" prefWidth="960.0" stylesheets="@css/main.css" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller">
<children>
<AnchorPane id="innerPane" layoutX="520.0" layoutY="270.0" prefHeight="256.0" prefWidth="426.0">
<children>
<Button id="rewind" fx:id="rewind" layoutX="14.0" layoutY="186.0" maxHeight="48.0" maxWidth="48.0" minHeight="48.0" minWidth="48.0" mnemonicParsing="false" onAction="#previous" prefHeight="48.0" prefWidth="48.0">
<graphic>
<ImageView fitHeight="47.0" fitWidth="63.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@img/previous.png" />
</image>
</ImageView>
</graphic></Button>
<Button id="play" fx:id="play" layoutX="181.0" layoutY="178.0" maxHeight="64.0" maxWidth="64.0" minHeight="64.0" minWidth="64.0" mnemonicParsing="false" onAction="#pause" prefHeight="64.0" prefWidth="64.0">
<graphic>
<ImageView fitHeight="62.0" fitWidth="69.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@img/pause.png" />
</image>
</ImageView>
</graphic></Button>
<Button id="forward" fx:id="forward" layoutX="364.0" layoutY="186.0" maxHeight="48.0" maxWidth="48.0" minHeight="48.0" minWidth="48.0" mnemonicParsing="false" onAction="#skip" prefHeight="48.0" prefWidth="48.0">
<graphic>
<ImageView fitHeight="48.0" fitWidth="47.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@img/skip.png" />
</image>
</ImageView>
</graphic></Button>
<Label fx:id="songName" layoutX="14.0" layoutY="14.0" prefHeight="64.0" prefWidth="405.0" style="-fx-font-family: 'Inter Extra Bold'; -fx-font-size: 40pt" text="SongName">
</Label>
<Label fx:id="artistName" layoutX="14.0" layoutY="78.0" prefHeight="17.0" prefWidth="274.0" style="-fx-font-family: 'Inter Semi Bold Italic'; -fx-font-size: 30pt" text="ArtistName">
</Label>
</children>
</AnchorPane>
<AnchorPane layoutX="14.0" layoutY="14.0" prefHeight="512.0" prefWidth="426.0">
<children>
<Label style="-fx-font-family: 'Inter Extra Bold'; -fx-font-size: 89pt" text="Coffea">
</Label>
<Hyperlink layoutX="14.0" layoutY="173.0" onAction="#songs" style="-fx-font-family: 'Inter Bold'; -fx-font-size: 34pt" text="Songs">
<graphic>
<ImageView fitHeight="37.56" fitWidth="37.56" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@img/music.png" />
</image>
</ImageView>
</graphic>
</Hyperlink>
<Hyperlink layoutX="14.0" layoutY="266.0" onAction="#playlists" style="-fx-font-family: 'Inter Bold'; -fx-font-size: 34pt" text="Playlists">
<graphic>
<ImageView fitHeight="37.56" fitWidth="37.56" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@img/playlist.png" />
</image>
</ImageView>
</graphic>
</Hyperlink>
</children>
</AnchorPane>
</children>
</AnchorPane>
Я ожидал, что текст на этикетке с fx: id songName
быть выведенным на консоль, однако я получаю исключение NullPointerException, как показано ниже:
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:8879)
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:3851)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
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:390)
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:174)
at java.base/java.lang.Thread.run(Thread.java:834)
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:566)
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:566)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
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 Controller.pause(Controller.java:25)
... 58 more
Очевидно, исключение вызвано строкой 25 (строкой, в которой я вызываю System.out.println(songName.getText());
).