添加fx:controller时的InvocationTargetException

时间:2015-09-05 01:48:29

标签: java javafx fxml

我查了几篇其他帖子,但未能找到答案。当我向一个按钮添加一个控制器和onAction标签时,我收到一个错误(InvocationTargetException)。也许有人能发现错误?如果我将窗格标记中的fx:controller和按钮的onaction属性删除,它会加载fxml表单。谢谢!

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="phonebook.UI.UI">
    <children>
      <TableView layoutX="199.0" prefHeight="350.0" prefWidth="401.0">
        <columns>
          <TableColumn prefWidth="106.0" text="First Name" />
          <TableColumn prefWidth="90.0" text="Last Name" />
            <TableColumn prefWidth="110.0" text="Number" />
            <TableColumn prefWidth="94.0" text="Address" />
        </columns>
      </TableView>
      <TextField fx:id="txtPhone_number" layoutX="37.0" layoutY="275.0" promptText="Phone Number" />
      <TextField fx:id="txtAddress" layoutX="37.0" layoutY="310.0" promptText="Address" />
      <Button fx:id="btnAdd" layoutX="134.0" layoutY="355.0" mnemonicParsing="false" onAction="#btnAddPerson" text="Add" />
      <TextField fx:id="txtSearch" layoutX="37.0" layoutY="82.0" promptText="Search by name" />
      <Button layoutX="127.0" layoutY="120.0" mnemonicParsing="false" text="Search" />
      <Button layoutX="450.0" layoutY="355.0" mnemonicParsing="false" text="Update" />
      <Button layoutX="517.0" layoutY="355.0" mnemonicParsing="false" text="Remove" />
      <TextField fx:id="txtLastName" layoutX="37.0" layoutY="240.0" promptText="Last name" />
      <TextField fx:id="txtFirstName" layoutX="37.0" layoutY="205.0" promptText="First name" />
      <Label layoutX="14.0" layoutY="14.0" text="Phone Book" textFill="#aeacac">
         <font>
            <Font size="31.0" />
         </font>
      </Label>
   </children>
</Pane>

和...

package phonebook.UI;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import static javafx.application.ConditionalFeature.FXML;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;


public class UI implements Initializable{

    //FXML Variables
    @FXML
    private TextField txtFirstName;
    @FXML
    private TextField txtLastName;
    @FXML
    private TextField txtPhone_number;
    @FXML
    private TextField txtAddress;
    @FXML 
    private Button btnAdd;

    //regular variables
    private Stage ms;

    public UI(Stage stage) {
        this.ms = stage;
    }

    public void stageOne() throws Exception{

        Parent root = FXMLLoader.load(getClass().getResource("UI.fxml"));
        Scene scene = new Scene(root);

        //set the stage and scene
        ms.setTitle("Phone Book.");
        ms.setScene(scene);
        ms.show();
    }

    @FXML
    protected void btnAddPerson(ActionEvent event) throws Exception {

    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

错误:

run:
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:497)
    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:497)
    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$156(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: 
/C:/Users/Ohlaph/Documents/NetBeansProjects/PhoneBook/build/classes/phonebook/UI/UI.fxml:8

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:932)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
    at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at phonebook.UI.UI.stageOne(UI.java:41)
    at phonebook.PhoneBook.start(PhoneBook.java:17)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
    ... 1 more
Caused by: java.lang.InstantiationException: phonebook.UI.UI
    at java.lang.Class.newInstance(Class.java:427)
    at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
    ... 23 more
Caused by: java.lang.NoSuchMethodException: phonebook.UI.UI.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.newInstance(Class.java:412)
    ... 25 more
Exception running application phonebook.PhoneBook
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

1 个答案:

答案 0 :(得分:0)

btnAddPerson没有访问修饰符,因此是“包私有”。 如果从不同的包调用它,它将不可见。 你有没有试过它protected

你也应该

@FXML private Button btnAdd;

编辑:

Caused by: java.lang.NoSuchMethodException: phonebook.UI.UI.<init>() 意味着你需要一个没有 Stage参数的公共构造函数。加上这个:

public UI(){
}

你可能想要

public setStage(Stage s){
  this.ms=s;
}

你需要另一种方式来设置舞台。 这是因为UI构造是自动完成的,因此FX无法知道在构造时传递的stage参数。你需要稍后再设置它。