尝试通过ActionEvent调用方法时发生NullPointerException

时间:2019-04-17 22:21:20

标签: javafx scenebuilder

当用户在我的GUI中单击HBox时,我正在尝试使方法运行。但是,每当用户单击HBox时,程序就会崩溃并返回NullPointerException。该程序甚至无法在崩溃前运行该方法。

这是控制器的代码:

package Controllers;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.shape.SVGPath;

public class MainController {

    @FXML
    private ImageView nowPlayingImage;

    @FXML
    private Label nowPlayingSong;

    @FXML
    private Label nowPlayingArtist;

    @FXML
    private HBox mediaControls;

    @FXML
    private SVGPath shuffleButton;

    @FXML
    private SVGPath repeatButton;

    @FXML
    private SVGPath playButton;

    @FXML
    private SVGPath pauseButton;

    @FXML
    private SVGPath skipForward;

    @FXML
    private SVGPath queueButton;

    @FXML
    private Slider timeSlider;

    @FXML
    private HBox playlists;

    @FXML
    private HBox artists;

    @FXML
    private HBox albums;

    @FXML
    private HBox songs;

    @FXML
    private TextField searchBar;

    @FXML
    private ScrollPane subView;

    private OpeningFXLMLTest main;

    @FXML
    public void playListsScene(MouseEvent event) {
        System.out.println("Test Text");
        try {
            main.pleaseWork(); 
        } catch (Exception e) {
        e.printStackTrace();
    }

}

以下是GUI从其中打开的主要方法的代码:

package Controllers;

import java.io.IOException;

import javafx.application.*;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class OpeningFXLMLTest extends Application{

    private Stage mainStage;
    private BorderPane mainLayout;
    private MainController mainController;
    private Scene scene;

    public static void main(String[] args) {

        launch(args);

    }

    @Override
    public void start(Stage stage) throws Exception {
        mainStage = stage;
        stage.setTitle("Music Player");

        showMainView();



    }

    public void showMainView() throws Exception {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("Main.fxml"));

        mainLayout = loader.load();

        scene = new Scene(mainLayout);
        mainStage.setScene(scene);
        mainStage.show();
    }

    public void pleaseWork() {
        System.out.println("yay");
    }



}

这是堆栈跟踪:

Test Text
java.lang.NullPointerException
    at Controllers.MainController.playListsScene(MainController.java:74)
    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)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
    at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    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(Unknown Source)

我还是JavaFX的新手,所以我可能会缺少一些显而易见的东西。

0 个答案:

没有答案