java.lang.reflect.InvocationTargetException when i try to change the label's text value

StackOverflow https://stackoverflow.com/questions/23276425

  •  09-07-2023
  •  | 
  •  

質問

Here is a simple label project for javafx using fxml with netbeans. The problem is, when I use

StartFomulars.setText("abc");

It just gets an exception given below.

Here is the code for main,

package main;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class Start extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Prove Controller = new Prove();
        Controller.launchController(stage);
    }

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

}

The fxml,

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1080.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
  <children>
    <GridPane layoutX="0.0" layoutY="0.0" prefHeight="720.0" prefWidth="1080.0">
      <children>
        <GridPane GridPane.columnIndex="0" GridPane.rowIndex="0">
          <columnConstraints>
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          </columnConstraints>
          <rowConstraints>
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          </rowConstraints>
        </GridPane>
        <GridPane GridPane.columnIndex="1" GridPane.rowIndex="1">
          <children>
            <GridPane GridPane.columnIndex="0" GridPane.rowIndex="0">
              <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
              </columnConstraints>
              <rowConstraints>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
              </rowConstraints>
            </GridPane>
          </children>
          <columnConstraints>
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          </columnConstraints>
          <rowConstraints>
            <RowConstraints maxHeight="282.0" minHeight="10.0" prefHeight="282.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="281.0" minHeight="10.0" prefHeight="281.0" vgrow="SOMETIMES" />
          </rowConstraints>
        </GridPane>
        <GridPane GridPane.columnIndex="0" GridPane.rowIndex="1">
          <children>
            <Label text="Given: " GridPane.columnIndex="0" GridPane.rowIndex="0" />
            <Label text="Prove: " GridPane.columnIndex="0" GridPane.rowIndex="1" />
            <Label text="Goal: " GridPane.columnIndex="0" GridPane.rowIndex="2" />
            <Label fx:id="StartFomulars" text="StartFomulars" GridPane.columnIndex="1" GridPane.rowIndex="0" />
            <Label fx:id="GoalFomular" text="GoalFomular" GridPane.columnIndex="1" GridPane.rowIndex="2" />
          </children>
          <columnConstraints>
            <ColumnConstraints hgrow="SOMETIMES" maxWidth="457.0" minWidth="10.0" prefWidth="125.0" />
            <ColumnConstraints hgrow="SOMETIMES" maxWidth="802.0" minWidth="10.0" prefWidth="790.0" />
          </columnConstraints>
          <rowConstraints>
            <RowConstraints maxHeight="188.0" minHeight="10.0" prefHeight="156.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="364.0" minHeight="10.0" prefHeight="364.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="186.0" minHeight="10.0" prefHeight="43.0" vgrow="SOMETIMES" />
          </rowConstraints>
        </GridPane>
      </children>
      <columnConstraints>
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="915.0" minWidth="10.0" prefWidth="915.0" />
        <ColumnConstraints hgrow="SOMETIMES" maxWidth="539.0" minWidth="10.0" prefWidth="165.0" />
      </columnConstraints>
      <rowConstraints>
        <RowConstraints maxHeight="359.0" minHeight="10.0" prefHeight="157.0" vgrow="SOMETIMES" />
        <RowConstraints maxHeight="582.0" minHeight="10.0" prefHeight="563.0" vgrow="SOMETIMES" />
      </rowConstraints>
    </GridPane>
  </children>
</AnchorPane>

Here is the code for controller,

package main;

import ast.LogicStatement;
import java.io.IOException;
import java.util.List;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;

/**
 *
 * @author zl2511
 */
public class Prove {

    private Parent parent;
    private Scene scene;
    private Stage stage;
    private List<LogicStatement> startStatements; 
    private LogicStatement goalStatement;

    @FXML
    private Label StartFomulars, GoalFomular; 

    public Prove() throws IOException { 

        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Prove.fxml"));
        //fxmlLoader.setController(this);
        try {
            parent = (Parent) fxmlLoader.load();

            scene = new Scene(parent);
        } catch (IOException e) {
        }

        StartFomulars.setText("abc");

    }


    public void launchController(Stage stage) {

        this.stage = stage;
        stage.setTitle("start");
        stage.setScene(scene);
        stage.setResizable(true);
        stage.hide();
        stage.show();

    }

    public void redirectprove(Stage stage, List<LogicStatement> startFormulas, LogicStatement goalFormula) { 

        this.startStatements = startFormulas; 
        this.goalStatement = goalFormula; 

        stage.setScene(scene);
        stage.hide();
        stage.show();

    }
}

Here is the exception,

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:483)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:367)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:305)
    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:483)
    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:894)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
役に立ちましたか?

解決

Your structure is pretty unusual. It's more normal to structure an Application class, FXML file, and controller as shown in the tutorial.

As you have things set up, there is no connection between your controller class and the FXML. So the fields annotated @FXML never get initialized.

The way this is intended to work, is that the FXMLLoader's load(...) method will parse the FXML file. If there is a controller set on the loader, then any elements with a fx:id attribute defined will be injected into matching @FXML annotated fields in the controller.

There are basically two ways of setting a controller on the FXMLLoader. The first is to have a fx:controller attribute defined on the root element of the FXML file. This will cause the FXML loader to create a new instance of the specified class, and use that as its controller. Note this won't work in your case, because you want the controller to be the Prove instance you have already created, not a new instance of the Prove class.

The second way is to create an FXMLLoader instance (instead of relying on the static FXMLLoader.load(URL) method you are currently using), and call setController(...) on that instance. So (I think) uncommenting the

//fxmlLoader.setController(this);

line will get this to work.

You may need to separate the declarations of the fields too, i.e.:

@FXML
private Label StartFomulars ;
@FXML
private Label GoalFomular; 

(It might work without that change, I've never tried it the way you have it.)

You should probably also take out the try {...} catch (...) {...} structure in the constructor for Prove (since you declare it throws IOException anyway), or at least do something in the catch block. Right now if an exception occurs loading the FXML you won't know about it, and you will just plough ahead and try and set the text of the label.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top