我有一个运行良好的FXML文件“ Students”,我在其中有一个表视图,并且编写了一个“ showData()”方法来从mySQL数据库中检索数据并填充它。当我将此方法放在我的“ studentController”类的“ initialise()”方法中时,此方法效果很好。我的问题是在“学生” FXML文件中有一个按钮,当我单击它时,应该打开一个新的FXML文件“ StudentForm”。这给了我那个错误
javafx.fxml.LoadException:
/C:/Users/USP/Desktop/School%20Management%20System/SchoolManagementSystem1/bin/home/StudentForm.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 home.StudentController.loadStage(StudentController.java:189)
at home.StudentController.addButtonClick(StudentController.java:113)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at home.StudentController.showData(StudentController.java:159)
at home.StudentController.initialize(StudentController.java:203)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 67 more
令人惊讶的是,当我从“ StudentController”的“ initialize()”中删除“ showData()”方法时,相同的FXML文件可以完美运行
这是StudentController类
package home;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
import com.sun.glass.ui.Window;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
public class StudentController implements Initializable {
static final String DB_URL = "jdbc:mysql://localhost/schoolsystem?useSSL=false";
static final String USER = "root";
static final String PASSWORD = "";
@FXML
private TableView<StudentsTable> Student_Table;
@FXML
private TableColumn<StudentsTable, Integer> StudentCol_ID;
@FXML
private TableColumn<StudentsTable, String> StudentCol_Name;
@FXML
private TableColumn<StudentsTable, Integer> StudentCol_Age;
@FXML
private TableColumn<StudentsTable, String> StudentCol_Major;
@FXML
private TableColumn<StudentsTable, Double> StudentCol_GPA;
@FXML
private Button Student_Add;
@FXML
private Button Student_Edit;
@FXML
private Button Student_Delete;
@FXML
private Button StudentForm_Submit;
@FXML
private TextField StudentForm_ID;
@FXML
private TextField StudentForm_Name;
@FXML
private TextField StudentForm_Age;
@FXML
private TextField StudentForm_Major;
ObservableList<StudentsTable> obList = FXCollections.observableArrayList();
public void addButtonClick(ActionEvent mouseEvent) {
loadStage("/home/StudentForm.fxml");
}
public void handleSubmit(ActionEvent actionEvent) throws SQLException {
// close
{
}
int ID = Integer.parseInt(StudentForm_ID.getText());
String Name = StudentForm_Name.getText();
int Age = Integer.parseInt(StudentForm_Age.getText());
String Major = StudentForm_Major.getText();
Student student = new Student(ID, Name, Age, Major);
// Database
Connection conn = null;
PreparedStatement stmt = null;
{
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
stmt = conn.prepareStatement("insert into students values(?,?,?,?,?)");
stmt.setInt(1, ID);
stmt.setString(2, Name);
stmt.setInt(3, Age);
stmt.setString(4, Major);
stmt.setDouble(5, 0);
stmt.executeUpdate();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
((Node)(actionEvent.getSource())).getScene().getWindow().hide();
}
public void showData() {
Connection conn = null;
PreparedStatement stmt = null;
{
System.out.println(" sa "+StudentCol_ID.getText());
StudentCol_ID.setCellValueFactory(new PropertyValueFactory<>("Student_ID"));
StudentCol_Name.setCellValueFactory(new PropertyValueFactory<>("Student_Name"));
StudentCol_Age.setCellValueFactory(new PropertyValueFactory<>("Student_Age"));
StudentCol_Major.setCellValueFactory(new PropertyValueFactory<>("Student_Major"));
StudentCol_GPA.setCellValueFactory(new PropertyValueFactory<>("Student_GPA"));
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
ResultSet rs = conn.createStatement().executeQuery("Select * from students");
while (rs.next()) {
obList.add(new StudentsTable(rs.getInt("Student_ID"), rs.getString("Student_Name"),
rs.getInt("Student_Age"), rs.getString("Student_Major"), rs.getDouble("Student_GPA")));
}
Student_Table.setItems(obList);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void loadStage(String fxml) {
try {
Parent root = FXMLLoader.load(getClass().getResource(fxml));
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.setResizable(false);
stage.getIcons().add(new Image(new FileInputStream("C:\\Users\\USP\\Pictures\\aa.png")));
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void initialize(URL location, ResourceBundle resources) {
showData();
}
}
请注意,StudentController中的所有ID都是正确的。