2017-05-30 3 views
0

ich die folgende Fehlermeldung geschrieben im Code immer unterJava Fehler: Ausnahme in Anwendungsstartmethode

Ich versuche, eine Taste, um Text anzuzeigen, wenn darauf geklickt. Ich bin mir nicht sicher, was den Fehler verursacht

Dies ist der Code:

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!"); 
     } 
     } 
    } 

Dies ist der Fehler:

----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 = [email protected] 
    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. 

Antwort

0

Das erste, was Sie tun müssen, ist bewegen text = new Text(); außerhalb der Ereignishandler. Das zweite, was Sie tun müssen, ist, den Ereignishandler an die Schaltfläche anzuhängen.

Der Grund für Ihren Fehler liegt darin, dass Sie den Text dem StackPane hinzugefügt haben, aber Sie erstellen das Textobjekt nie, bis die Schaltfläche gedrückt wird. Die Lösung erstellt also sofort das Text-Objekt, damit es zum StackPane hinzugefügt werden kann. Dann wird der Text des Textobjekts aktualisiert, wenn die Schaltfläche gedrückt wird.

Example 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!"); 
} 

Example 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(); 
}