Java错误:Application start方法中的异常

时间:2017-05-30 15:43:35

标签: java

我在下面发布的代码中收到以下错误

我想点击一个按钮来显示文字。我不确定导致错误的原因

这是代码:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.text.*;

public class javafx extends Application implements EventHandler<ActionEvent> {

    Button button;
    Text text;

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

    @Override
    public void start(Stage primaryStage) {

        primaryStage.setTitle("My first window");
        button = new Button();
        button.setText("Click meh bro!");
        button.setTranslateX(0);
        button.setTranslateY(-100);
        button.setOnAction(this);

        StackPane layout = new StackPane();
        layout.getChildren().addAll(text , button);
        Scene scene = new Scene(layout, 600, 300);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    @Override
    public void handle(ActionEvent shitgoindown) {
        if (shitgoindown.getSource() == button) {
                          //Creating a Text object 
           text = new Text();      
           text.setFont(new Font(31));
           text.setText("Hey bro , danks for clicken meh!");
         }
      }
    }

这是错误:

 ----jGRASP exec: java javafx
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException: Children: child node is null: parent = StackPane@5f2d5162
    at javafx.scene.Parent$2.onProposedChange(Parent.java:435)
    at com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:234)
    at com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:103)
    at javafx.start(javafx.java:31)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application javafx

 ----jGRASP wedge: exit code for process is 1.
 ----jGRASP: operation complete.

1 个答案:

答案 0 :(得分:0)

您需要做的第一件事是将text = new Text();移到事件处理程序之外。您需要做的第二件事是将事件处理程序附加到按钮。

出错的原因是您将文本添加到StackPane,但在按下按钮之前,您永远不会创建Text对象。因此解决方案是立即创建Text对象,以便将其添加到StackPane。然后在按下按钮时更新Text对象的文本。

  

示例1:

Button button;
Text text;

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

@Override
public void start(Stage primaryStage) {

    text = new Text(); 
    text.setFont(new Font(31));

    primaryStage.setTitle("My first window");
    button = new Button();
    button.setText("Click meh bro!");
    button.setTranslateX(0);
    button.setTranslateY(-100);
    button.setOnAction(this::handle);

    StackPane layout = new StackPane();
    layout.getChildren().addAll(text , button);
    Scene scene = new Scene(layout, 600, 300);
    primaryStage.setScene(scene);
    primaryStage.show();
}

private void handle(ActionEvent event) {
    // Button was clicked, do something...
    text.setText("Hey bro , danks for clicken meh!");
}
  

示例2:

Button button;
Text text;

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

@Override
public void start(Stage primaryStage) {

    text = new Text(); 
    text.setFont(new Font(31));

    primaryStage.setTitle("My first window");
    button = new Button();
    button.setText("Click meh bro!");
    button.setTranslateX(0);
    button.setTranslateY(-100);
    button.setOnAction((shitgoindown)->{               

       text.setText("Hey bro , danks for clicken meh!");
    });

    StackPane layout = new StackPane();
    layout.getChildren().addAll(text , button);
    Scene scene = new Scene(layout, 600, 300);
    primaryStage.setScene(scene);
    primaryStage.show();
}