即使我已正确绑定所有元素,为什么我的JavaFX控制器也会抛出NullPointer?

时间:2018-12-22 14:38:04

标签: java javafx scenebuilder

我正在为我的应用编写UI,并且我希望背景是模糊的图像,因此我将ImageView设置为背景,并且在代码的开头,它应该通过ImageView.setEffect()进行模糊处理,但是它抛出一个NullPointer异常。你能告诉我为什么吗?

我尝试检查是否有任何错误的绑定,但是我没有,因为那些绑定是由Scene Builder创建的,而且我不知道该怎么想...

这是控制器:

package application;

import javafx.fxml.FXML;
import javafx.scene.effect.GaussianBlur;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Text;

public class MainController {
@FXML
    private static ImageView BackgroundComponent;

    @FXML
    private static AnchorPane AppComponent;

    @FXML
    private static ImageView AppComponent_AppIcon;

    @FXML
    private static Text AppComponent_AppName;

    @FXML
    private static ImageView SystemComponent_BluetoothIcon;

    public static void setBackgroundBlur()
    {
         BackgroundComponent.setEffect(new GaussianBlur());
    }
}

这是.fxml:

<AnchorPane prefHeight="800" prefWidth="480" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
   <children>
      <ImageView fx:id="BackgroundComponent" pickOnBounds="true" preserveRatio="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <image>
            <Image url="@../../resources/proxy.duckduckgo.com.jpg" />
         </image>
         <viewport>
            <Rectangle2D />
         </viewport>
      </ImageView>
</AnchorPane>

这是完整的堆栈跟踪:

java.lang.NullPointerException
    at application.MainController.setBackgroundBlur(MainController.java:27)
    at application.Main.start(Main.java:16)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    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$147(WinApplication.java:177)
    at java.lang.Thread.run(Unknown Source)

不要认为这是必需的,但是您可能需要我的主要start():

Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
Scene scene = new Scene(root,800,480);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
MainController.setBackgroundBlur();
primaryStage.setFullScreen(true);
primaryStage.setTitle("Home Assistant");
primaryStage.setScene(scene);
primaryStage.show();

响应重复的标记: 我试过删除静态范围,但它仍会引发Null指针

0 个答案:

没有答案