当我尝试更改标签的文本值时,java.lang.reflect.InvocationTargetException [英] java.lang.reflect.InvocationTargetException when i try to change the label's text value

查看:234
本文介绍了当我尝试更改标签的文本值时,java.lang.reflect.InvocationTargetException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个使用fxml和netbeans的javafx的简单标签项目。
问题是,当我使用

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.

以下是main的代码,

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

}

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>

以下是控制器的代码,

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

    }
}

这是例外,

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


推荐答案

你的结构很不寻常。构造Application类,FXML文件和控制器更为正常,如教程

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

当你设置好东西时,你的控制器类和FXML之间没有任何联系。所以带注释的字段 @FXML 永远不会被初始化。

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.

这是打算工作的方式,是 FXMLLoader 加载( ...)方法将解析FXML文件。如果在加载器上设置了控制器,则定义了 fx:id 属性的任何元素将被注入匹配的 @FXML 控制器中带注释的字段。

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.

基本上有两种方法可以在 FXMLLoader 上设置控制器。第一种是在FXML文件的根元素上定义 fx:controller 属性。这将导致FXML加载程序创建指定类的新实例,并将其用作其控制器。请注意,这不适用于您的情况,因为您希望控制器是您已创建的 Prove 实例,而不是的新实例证明类。

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.

第二种方法是创建一个 FXMLLoader 实例(而不是依赖于您当前正在使用的 static FXMLLoader.load(URL)方法,并调用该实例上的setController(...)。所以(我认为)取消注释

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);

行将使其生效。

您可能还需要分隔字段的声明,即:

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.)

你应该也可以拿出 try {...} catch(...){...} Prove 的构造函数中的结构(因为你声明它抛出IOException ,或者至少做一些事情) catch 块。现在,如果加载FXML会发生异常,你就不会知道它了,你只需继续前进并尝试设置标签的文本。

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.

这篇关于当我尝试更改标签的文本值时,java.lang.reflect.InvocationTargetException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆