JavaFX编译错误

时间:2015-07-25 23:00:07

标签: javafx fxml

我是JavaFX的新手,我不确定这里出了什么问题。一直试图弄清楚几个小时。请帮我弄清楚我哪里出错!

错误跟踪

"C:\Program Files\Java\jdk1.8.0_31\bin\java" -Didea.launcher.port=7533 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\bin" -Dfile.encoding=windows-1252 -classpath "C:\Program Files\Java\jdk1.8.0_31\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_31\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_31\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_31\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_31\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_31\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_31\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_31\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_31\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_31\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_31\jre\lib\rt.jar;C:\Program Files\Java\jdk1.8.0_31\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_31\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_31\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_31\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_31\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_31\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_31\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_31\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_31\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_31\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_31\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_31\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\alloy.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\annotations.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\asm-all.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\asm-commons.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\asm-tree-3.0.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\asm.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\asm4-all.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\automaton.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\batik.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\boot.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\bootstrap.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\cglib-2.2.2.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\cli-parser-1.1.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\commons-codec-1.8.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\commons-httpclient-3.1-patched.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\commons-logging-1.1.3.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\commons-net-3.3.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\ecj-4.4.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\extensions.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\fluent-hc-4.3.6.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\forms_rt.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\freemarker.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\fxHelpBrowser.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\groovy-all-2.3.9.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\gson-2.3.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\guava-17.0.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\hamcrest-core-1.3.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\hamcrest-library-1.3.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\httpclient-4.3.6.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\httpcore-4.3.3.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\httpmime-4.3.6.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\icons.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\idea-jsp-openapi.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\idea.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\ideax.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\idea_rt.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\isorelax.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jasper21_rt.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jasper2_rt.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\javac2.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jaxen-1.1.3.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jayatana-1.2.4.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jcip-annotations.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jdkAnnotations.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jdom.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jettison-1.3.2.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jgoodies-common-1.2.1.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jgoodies-forms.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jgoodies-looks-2.4.2.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jh.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jing.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jna-utils.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jna.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jps-launcher.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jps-model.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jps-server.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jsch-0.1.51.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jsch.agentproxy.connector-factory.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jsch.agentproxy.core.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jsch.agentproxy.pageant.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jsch.agentproxy.sshagent.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jsch.agentproxy.usocket-jna.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jsch.agentproxy.usocket-nc.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jsp-api.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jsr166e.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jsr173_1.0_api.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\junit-4.11.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\jzlib-1.1.1.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\log4j.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\markdown4j-2.2.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\markdownj-core-0.4.2-SNAPSHOT.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\microba.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\miglayout-swing.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\nanoxml-2.2.3.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\nekohtml-1.9.14.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\netty-all-4.1.0.Beta4.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\openapi.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\optimizedFileManager.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\oromatcher.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\picocontainer.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\protobuf-2.5.0.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\proxy-vole_20131209.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\pty4j-0.4.15.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\purejavacomm.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\resolver.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\resources.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\resources_en.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\rhino-js-1_7R4.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\rngom-20051226-patched.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\sanselan-0.98-snapshot.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\serviceMessages.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\servlet-api.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\slf4j-api-1.7.10.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\slf4j-log4j12-1.7.10.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\snappy-in-java-0.3.1.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\swingx-core-1.6.2.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\trang-core.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\trove4j.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\trove4j_src.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\util.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\velocity.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\winp-1.21-patched.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\xbean.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\xerces.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\xml-apis.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\xmlrpc-2.0.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\xpp3-1.1.4-min.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\xstream-1.4.3.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\yjp-controller-api-redist.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\plugins\JavaEE\lib\gradle-javaee.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\plugins\JavaEE\lib\IntelliLang-javaee.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\plugins\JavaEE\lib\javaee-openapi.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\plugins\JavaEE\lib\resources_en.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\plugins\PersistenceSupport\lib\persistence-openapi.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\plugins\PersistenceSupport\lib\resources_en.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\plugins\DatabaseTools\lib\database-openapi.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\plugins\DatabaseTools\lib\jdba.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\plugins\DatabaseTools\lib\resources_en.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\plugins\CSS\lib\css-openapi.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\plugins\CSS\lib\resources_en.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\plugins\uml\lib\diagram-api.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\plugins\Spring\lib\spring-aop.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\plugins\Spring\lib\spring-api.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\plugins\Spring\lib\spring-eclipse.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\plugins\Spring\lib\spring-web.jar;C:\Users\Ethan\workspace\CSC 275 Lab 8\out\production\CSC 275 Lab 8" com.intellij.rt.execution.application.AppMain Lab8
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
    at com.sun.javafx.application.LauncherImpl$$Lambda$1/815033865.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: 
/C:/Users/Ethan/workspace/CSC%20275%20Lab%208/out/production/CSC%20275%20Lab%208/Lab8Layout.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2595)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2435)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3208)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3169)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3142)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3118)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3098)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3091)
    at Lab8.start(Lab8.java:14)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
    at com.sun.javafx.application.LauncherImpl$$Lambda$50/2020077337.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
    at com.sun.javafx.application.PlatformImpl$$Lambda$46/834634352.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/1656466025.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/1045693783.run(Unknown Source)
    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$141(WinApplication.java:102)
    at com.sun.glass.ui.win.WinApplication$$Lambda$38/891996806.run(Unknown Source)
    ... 1 more
Caused by: java.lang.NullPointerException
    at Brains.initialize(Brains.java:34)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2542)
    ... 22 more

Process finished with exit code 1

主类

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

public class Lab8 extends Application {

    public static void main(String[]args){
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("Lab8Layout.fxml"));
        primaryStage.setTitle("Student Interface");
        primaryStage.setScene(new Scene(root, 700,700));
        primaryStage.show();
    }
}

控制器类(称为大脑)

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import java.net.URL;
import java.util.ResourceBundle;

public class Brains implements Initializable {

    @FXML public TableView<Student> studentTable;
    ObservableList<Student> studentArray = FXCollections.observableArrayList();

    //First Name Column
    public TableColumn<Student,String> firstNameColumn = new TableColumn<>("First Name");
    //Last Name Column
    @FXML TableColumn<Student,String> lastNameColumn = new TableColumn<>("Last Name");
    //Major Column
    @FXML TableColumn<Student,String> majorColumn = new TableColumn<>("Major");
    //GPA Column
    @FXML TableColumn<Student,Double> gpaColumn = new TableColumn<>("GPA");
    //UIN Column
    @FXML TableColumn<Student,Integer> uinColumn = new TableColumn<>("UIN");
    //Net ID Column
    @FXML TableColumn<Student,Integer> netIDColumn = new TableColumn<>("Net ID");
    //Age Column
    @FXML TableColumn<Student,Integer> ageColumn = new TableColumn<>("Age");
    //Gender Column
    @FXML TableColumn<Student,String> genderColumn = new TableColumn<>("Gender");

    @Override
    public void initialize(URL location, ResourceBundle resources){
        studentTable.setItems(studentArray);

        firstNameColumn.setCellValueFactory(new PropertyValueFactory<>("firstName"));
        lastNameColumn.setCellValueFactory(new PropertyValueFactory<>("lastName"));
        majorColumn.setCellValueFactory(new PropertyValueFactory<>("major"));
        gpaColumn.setCellValueFactory(new PropertyValueFactory<>("gpa"));
        uinColumn.setCellValueFactory(new PropertyValueFactory<>("uin"));
        netIDColumn.setCellValueFactory(new PropertyValueFactory<>("netID"));
        ageColumn.setCellValueFactory(new PropertyValueFactory<>("age"));
        genderColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));

    }

    public void deleteStudent() {
        Student selectedStudent = studentTable.getSelectionModel().getSelectedItem();


    }

}

学生班

import javafx.beans.property.*;

public class Student {
    private String firstName;
    private String lastName;
    private String major;
    private double gpa;
    private int uin;
    private int netID = 0;
    private int age;
    private String gender;
    public static int counter = 1;

    public Student(String newFirstName, String newLastName, String newMajor, double newGpa, int newUin, int newAge, String newGender){
        firstName = newFirstName;
        lastName = newLastName;
        setMajor(newMajor);
        setGpa(newGpa);
        setUin(newUin);
        setAge(newAge);
        setGender(newGender);
        netID = counter++;
    }
    public Student(String newFirstName, String newLastName, int newNetID, String newMajor, double newGpa, int newUin, int newAge, String newGender){
        firstName = newFirstName;
        lastName = newLastName;
        setMajor(newMajor);
        setGpa(newGpa);
        setUin(newUin);
        netID = newNetID;
        setAge(newAge);
        setGender(newGender);
        this.netID = counter++;
    }
    public String getFirstName(){
        return firstName;
    }
    public String getLastName(){
        return lastName;
    }
    public int getID(){
        return netID;
    }
    public String getMajor() {
        return major;
    }
    public void setMajor(String major) {
        this.major = major;
    }
    public double getGpa() {
        return gpa;
    }
    public void setGpa(double gpa) {
        this.gpa = gpa;
    }
    public int getUin() {
        return uin;
    }
    public void setUin(int uin) {
        this.uin = uin;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {this.gender = gender; }
}

FXML文件

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

<BorderPane 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="Brains">
   <top>
      <VBox BorderPane.alignment="CENTER">
         <children>
            <MenuBar>
              <menus>
                <Menu mnemonicParsing="false" text="File">
                  <items>
                        <MenuItem mnemonicParsing="false" text="Save" />
                        <MenuItem mnemonicParsing="false" text="Load Students" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Edit">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Delete" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Help">
                  <items>
                    <MenuItem mnemonicParsing="false" text="About" />
                  </items>
                </Menu>
              </menus>
            </MenuBar>
            <HBox prefHeight="0.0" prefWidth="595.0">
               <children>
                  <TextField minWidth="80.0" promptText="First Name">
                     <padding>
                        <Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
                     </padding>
                     <HBox.margin>
                        <Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
                     </HBox.margin>
                  </TextField>
                  <TextField minWidth="80.0" promptText="Last Name">
                     <padding>
                        <Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
                     </padding>
                     <opaqueInsets>
                        <Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
                     </opaqueInsets>
                     <HBox.margin>
                        <Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
                     </HBox.margin>
                  </TextField>
                  <TextField minWidth="80.0" promptText="Major">
                     <padding>
                        <Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
                     </padding>
                     <HBox.margin>
                        <Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
                     </HBox.margin>
                  </TextField>
                  <TextField minWidth="20.0" promptText="GPA">
                     <padding>
                        <Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
                     </padding>
                     <HBox.margin>
                        <Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
                     </HBox.margin>
                  </TextField>
                  <TextField promptText="UIN">
                     <HBox.margin>
                        <Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
                     </HBox.margin>
                     <padding>
                        <Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
                     </padding>
                  </TextField>
                  <TextField minWidth="20.0" promptText="Age">
                     <padding>
                        <Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
                     </padding>
                     <HBox.margin>
                        <Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
                     </HBox.margin>
                  </TextField>
                  <TextField minWidth="30.0" promptText="Gender">
                     <padding>
                        <Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
                     </padding>
                     <HBox.margin>
                        <Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
                     </HBox.margin>
                  </TextField>
                  <Pane prefHeight="29.0" prefWidth="97.0" HBox.hgrow="ALWAYS" />
                  <Button minWidth="85.0" mnemonicParsing="false" text="Add Student" HBox.hgrow="ALWAYS" />
               </children>
               <padding>
                  <Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
               </padding>
               <VBox.margin>
                  <Insets bottom="3.0" left="3.0" right="10.0" top="3.0" />
               </VBox.margin>
            </HBox>
         </children>
      </VBox>
   </top>
   <center>
      <TableView id="studentTable" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
        <columns>
          <TableColumn text="First Name" id="firstNameColumn"/>
          <TableColumn text="Last Name" id="lastNameColumn" />
          <TableColumn text="Major" id="majorColumn" />
          <TableColumn text="GPA" id="gpaColumn" />
          <TableColumn text="UIN" id="uinColumn" />
          <TableColumn text="Age" id="ageColumn" />
          <TableColumn text="Gender" id="genderColumn" />
        </columns>
      </TableView>
   </center>
   <bottom>
      <VBox BorderPane.alignment="CENTER">
         <children>
            <HBox spacing="5.0">
               <children>
                  <Button id="deleteButton" mnemonicParsing="false" onAction="#deleteStudent" text="Delete Student" />
               </children>
               <padding>
                  <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
               </padding>
            </HBox>
            <TextField editable="false" text="Status Bar" />
         </children>
      </VBox>
   </bottom>
</BorderPane>

2 个答案:

答案 0 :(得分:2)

为了使其正常正常运作,您需要:

  1. 在FXML文件中的所有引用中设置fx:id,然后使用@FXML进行注释。您正在使用id。查看this

  2. public

  3. 中移除studentTable
  4. Brains上删除所有初始化。由于注释,它们是自动完成的。阅读以下任何教程:

  5. 但是,简而言之,FXMLLoader负责加载fxml文件,找到标有fx:controller的控制器,并创建该类的实例,注入标有{{1}的对象}并使用fx:id进行注释,最后调用@FXML

    1. 您在FXML上缺少initialize()

    2. 使用netIDColumn注释deleteStudent()

    3. 现在它会毫无问题地运行。

      然后我建议您使用@FXML上的属性,例如Student。看看this

答案 1 :(得分:-2)

通常情况下,堆栈跟踪会告诉您错误:

  

引起:java.lang.NullPointerException

它甚至可以告诉你在哪里:

  

at Brains.initialize(Brains.java:34)

我的猜测是,第34行是studentTable.setItems(studentArray);,当运行此代码时,studentTable为空。

在你的Brains课程中,所有其他字段都已初始化,但studentTable不是。尝试以与其他字段类似的方式初始化它,看看是否能解决问题。