首页 > 解决方案 > JavaFX (java.lang.NullPointerException)

问题描述

这是在我有 setItems(oblist) 的 TableController 文件中启动 FXML 的文件,这是错误的来源。它说我的代码中的某些内容指向 NULL。我正在尝试使用 mySQL 数据库中的数据插入 GUI,但无法启动 GUI。这是我一直关注的 youtube 视频的链接。https://www.youtube.com/watch?v=LoiQVoNil9Q

package cheapflights.system.gui;

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

public class JavaFXDB extends Application{

    @Override
    public void start(Stage stage) throws Exception {
        // TODO Auto-generated method stub
        Parent root = FXMLLoader.load(getClass().getResource("GUIGUI.fxml"));

        Scene scene = new Scene(root);

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

    public static void main(String[] args) { 


        launch(args);
}

}

这是主要代码/控制器

package cheapflights.system.gui;

import javafx.fxml.FXML;

import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.mysql.cj.conf.IntegerProperty;

import javafx.fxml.Initializable;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.*;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

public class TableController implements Initializable {

    @FXML
    private TableView<ModelTable> table;
    @FXML
    private TableColumn<ModelTable,IntegerProperty> col_flightID;
    @FXML
    private TableColumn<ModelTable,StringProperty> col_departingCity;
    @FXML
    private TableColumn<ModelTable,StringProperty> col_arrivalCity;
    @FXML
    private TableColumn<ModelTable,StringProperty> col_departureTime;
    @FXML
    private TableColumn<ModelTable,StringProperty> col_arrivalTime;
    @FXML
    private TableColumn<ModelTable,StringProperty> col_flightDate;
    @FXML
    private TableColumn<ModelTable,IntegerProperty> col_Quantity;

    ObservableList<ModelTable> oblist = FXCollections.observableArrayList();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // TODO Auto-generated method stub

        Connection myConn = null;
        Statement myStmt = null;
        ResultSet myRs = null;
        try {
            myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo", "flightuser" , "flightuser");

            myStmt = myConn.createStatement();

            // 3. Execute SQL query
            myRs = myStmt.executeQuery("select * from Flight");

            while (myRs.next()) {
                oblist.add(new ModelTable(myRs.getInt("flightID"), myRs.getString("departingCity"), 
                        myRs.getString("arrivalCity"), myRs.getString("departureTime"), myRs.getString("arrivalTime"), myRs.getString("flightDate"), myRs.getInt("Quantity") ));
            }
        } catch (SQLException ex) {
            // TODO Auto-generated catch block
            Logger.getLogger(TableController.class.getName()).log(Level.SEVERE, null, ex);
        }




        col_flightID.setCellValueFactory(new PropertyValueFactory<>("flightID"));
        col_departingCity.setCellValueFactory(new PropertyValueFactory<>("departingCity"));
        col_arrivalCity.setCellValueFactory(new PropertyValueFactory<>("arrivalCity"));
        col_departureTime.setCellValueFactory(new PropertyValueFactory<>("departureTime"));
        col_arrivalTime.setCellValueFactory(new PropertyValueFactory<>("arrivalTime"));
        col_flightDate.setCellValueFactory(new PropertyValueFactory<>("flightDate"));
        col_Quantity.setCellValueFactory(new PropertyValueFactory<>("Quantity"));

    table.setItems(oblist);
    }

}

这是模型表

package cheapflights.system.gui;

public class ModelTable {

    private int flightID;
    private String departingCity;
    private String arrivalCity;
    private String departureTime;
    private String arrivalTime;
    private String flightDate;
    private int Quantity;



    public ModelTable(int flightID, String departingCity, String arrivalCity,String departureTime, String arrivalTime, String flightDate, int Quantity) {
        this.flightID = flightID;
        this.departingCity = departingCity;
        this.arrivalCity= arrivalCity;
        this.departureTime = departureTime;
        this.arrivalTime = arrivalTime;
        this.flightDate = flightDate;
        this.Quantity = Quantity;
    }

    public int getFlight() {
        return flightID;
    }

    public void setFlight(int flightID) {
        this.flightID = flightID;
    }

    public String getarrivalTime() {
        return arrivalTime;
    }

    public void setarrivalTime(String arrivalTime) {
        this.arrivalTime = arrivalTime;
    }

    public String getdepartureTime() {
        return departureTime;
    }

    public void setdepatureTime(String departureTime) {
        this.departureTime = departureTime;
    }

    public String getdepartingCity() {
        return departingCity;
    }

    public void setdepartingCity(String departingCity) {
        this.departingCity = departingCity;
    }

    public String getarrivalCity() {
        return arrivalCity;
    }
    public void setarrivalCity(String arrivalCity) {
        this.arrivalCity = arrivalCity;
    }

    public String getflightDate() {
        return flightDate;
    }
    public void setflightDate(String flightDate) {
        this.flightDate = flightDate;
    }

    public int getQuantity() {
        return Quantity;
    }

    public void setquntity(int Quantity) {
        this.Quantity = Quantity;
    }


}

这是 FXML 文件

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

<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller = "cheapflights.system.gui.TableController">
   <children>
      <TableView layoutX="-222.0" layoutY="-32.0" prefHeight="410.0" prefWidth="562.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <columns>
          <TableColumn fx:id="col_flightID" prefWidth="71.0" text="flightID" />
          <TableColumn fx:id="col_departingCity" prefWidth="71.0" text="departingCity" />
            <TableColumn fx:id="col_arrivalCity" prefWidth="89.0" text="arrivalCity" />
            <TableColumn fx:id="col_departureTime" prefWidth="75.0" text="departureTime" />
            <TableColumn fx:id="col_arrivalTime" prefWidth="95.0" text="arrivalTime" />
            <TableColumn fx:id="col_flightDate" minWidth="0.0" prefWidth="82.0" text="flightDate" />
            <TableColumn fx:id="col_Quantity" minWidth="0.0" prefWidth="73.0" text="Quantity" />
        </columns>
      </TableView>
   </children>
</AnchorPane>

这是运行时错误

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$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException: 
/home/dvd86/git/cis3270-project/TermProject/bin/cheapflights/system/gui/GUIGUI.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    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 cheapflights.system.gui.JavaFXDB.start(JavaFXDB.java:14)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
    ... 1 more
Caused by: java.lang.NullPointerException
    at cheapflights.system.gui.TableController.initialize(TableController.java:87)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 17 more
Exception running application cheapflights.system.gui.JavaFXDB

这是运行时错误中显示的特定错误

   Caused by: java.lang.NullPointerException
        at cheapflights.system.gui.TableController.initialize(TableController.java:87)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
        ... 17 more
    Exception running application cheapflights.system.gui.JavaFXDB

标签: javajavafxtableview

解决方案


推荐阅读