В качестве домашней работы я создаю приложение, которое принимает информацию о клиенте и регистрирует ее с использованием базы данных derby и javafx. После регистрации новый клиент появится в виде таблицы. Я пытался добавить строки в просмотр таблицы, но java.lang.reflect.InvocationTargetException
продолжает появляться, и я понятия не имею, что происходит. Я хочу получить данные из моей базы данных derby и показать их в виде таблицы в моем приложении. Когда вы открываете приложение, появляется возможность добавить нового клиента. При нажатии на опцию добавления нового клиента, появляется новое окно, которое запрашивает информацию о новом клиенте. Я сделал отдельный файл F XML для нового окна ввода клиента и отдельный контроллер для него (здесь не показан ни один). После ввода всей необходимой информации в окне ввода клиента вы нажимаете кнопку отправки, и введенная информация будет вставлена в базу данных derby, затем класс NewCustomerEntryController вызовет метод handleNewSubmit в классе «RetailSystemController» (показан ниже). Пожалуйста, посмотрите мой код ниже и всплывающее исключение.
Это основной класс, который переопределяет метод запуска javafx, и я установил свой драйвер и соединение URL для derby в этом классе
package retailsystem;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DatabaseMetaData;
public class main extends Application {
private static Connection conn;
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("retailsystem.fxml"));
primaryStage.setTitle("Retail System Management");
primaryStage.setScene(new Scene(root, 600, 400));
setUpDB();
primaryStage.show();
}
private void setUpDB() throws Exception {
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
String dbName = "RetailSystemDB";
String connectionURL = "jdbc:derby:" + dbName + ";create=true";
String createCustomerString = "CREATE TABLE Customer (FirstName VARCHAR (50) NOT "
+ "NULL, LastName VARCHAR(50) NOT NULL, StreetAddress VARCHAR(50) "
+ "NOT NULL, City VARCHAR(20) NOT NULL, State VARCHAR(12) NOT NULL, "
+ "Zipcode VARCHAR(5), Gender VARCHAR(6))";
Class.forName(driver);
conn = DriverManager.getConnection(connectionURL);
Statement stmt = conn.createStatement();
DatabaseMetaData dbm = conn.getMetaData();
ResultSet checkCustomer = dbm.getTables(null, null, "CUSTOMER", null);
if (!checkCustomer.next()) {
stmt.executeUpdate(createCustomerString);
}
stmt.close();
}
public static Connection getConn() {
return conn;
}
public static void main(String[] args) {
launch(args);
}
}
Код ниже является контроллером для окна ввода нового клиента:
package retailsystem;
import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Scanner;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.stage.Stage;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javafx.scene.control.TableView;
public class CustomerInformationController implements Initializable {
@FXML
private TextField fEntry;
@FXML
private TextField lEntry;
@FXML
private TextField aEntry;
@FXML
private TextField cEntry;
@FXML
private ComboBox<String> sEntry;
@FXML
private RadioButton male;
@FXML
private RadioButton female;
@FXML
private TextField zEntry;
@FXML
private Button submitBTN;
@FXML
private Button cancelBtn;
private static person customer;
ToggleGroup group = new ToggleGroup();
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
String[] stateList = getStateList();
for (int i = 0; i < stateList.length; i++) {
sEntry.getItems().add(stateList[i]);
}
sEntry.setValue(stateList[0]);
male.setToggleGroup(group);
female.setToggleGroup(group);
}
@FXML
private void handleSubmit(ActionEvent event) throws Exception {
RadioButton selectedBtn = (RadioButton) group.getSelectedToggle();
customer = new person(fEntry.getText(), lEntry.getText(),
aEntry.getText(), cEntry.getText(), sEntry.getValue(), zEntry.getText(),
selectedBtn.getText());
Connection conn = main.getConn();
PreparedStatement psInsert = conn.prepareStatement("INSERT INTO CUSTOMER "
+ "(FirstName, LastName, StreetAddress, City, State, Zipcode, "
+ "Gender) VALUES(?,?,?,?,?,?,?)");
psInsert.setString(1, customer.getFirstName());
psInsert.setString(2, customer.getLastName());
psInsert.setString(3, customer.getAddress());
psInsert.setString(4, customer.getCity());
psInsert.setString(5, customer.getState());
psInsert.setString(6, customer.getZipcode());
psInsert.setString(7, customer.getGender());
psInsert.executeUpdate();
RetailSystemController retailcontroller = new RetailSystemController();
retailcontroller.handleNewSubmit();
psInsert.close();
}
}
Код ниже - мой файл f xml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="retailsystem.RetailSystemController">
<center>
<TableView fx:id="tView" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<columns>
<TableColumn fx:id="fName" prefWidth="90.0" text="First Name" />
<TableColumn fx:id="lName" prefWidth="90.0" text="Last Name" />
<TableColumn fx:id="address" prefWidth="147.0" text="Street Address" />
<TableColumn fx:id="city" prefWidth="87.0" text="City" />
<TableColumn fx:id="state" prefWidth="64.0" text="State" />
<TableColumn fx:id="zip" prefWidth="62.0" text="Zip Code" />
<TableColumn fx:id="gender" prefWidth="70.0" text="Gender" />
</columns>
</TableView>
</center>
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="Customer">
<items>
<MenuItem mnemonicParsing="false" onAction="#handleCustomerAddNew" text="Add New" />
<MenuItem mnemonicParsing="false" onAction="#handleCustomerListAll" text="List All" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Employee">
<items>
<MenuItem mnemonicParsing="false" onAction="#handleEmployeeAddNew" text="Add New " />
<MenuItem mnemonicParsing="false" onAction="#handleEmployeeListAll" text="List All" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Merchandise">
<items>
<MenuItem mnemonicParsing="false" onAction="#handleMerchandiseAddNew" text="Add New" />
<MenuItem mnemonicParsing="false" onAction="#handleMerchandiseListAll" text="List All" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
</BorderPane>
Код ниже - это Контроллер класса для упомянутого выше файла f xml, в этом классе довольно много обработчиков, но я сейчас сосредоточен только на «handleCustomerAddnew» и «handleNewSubmit», так как я не написал много кода для остальных. Проблема возникает в методе handleNewSubmit этого класса :
package retailsystem;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import javafx.stage.Window;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
public class RetailSystemController implements Initializable {
@FXML
private TableColumn fName;
@FXML
private TableColumn lName;
@FXML
private TableColumn address;
@FXML
private TableColumn city;
@FXML
private TableColumn state;
@FXML
private TableColumn zip;
@FXML
private TableColumn gender;
@FXML
private TableView<person> tView;
public void initialize(URL url, ResourceBundle rb) {
//ToDo
fName.setCellValueFactory(new PropertyValueFactory<person, String>("firstName"));
lName.setCellValueFactory(new PropertyValueFactory<person, String>("lastName"));
address.setCellValueFactory(new PropertyValueFactory<person, String>("address"));
city.setCellValueFactory(new PropertyValueFactory<person, String>("city"));
state.setCellValueFactory(new PropertyValueFactory<person, String>("state"));
zip.setCellValueFactory(new PropertyValueFactory<person, String>("Zipcode"));
gender.setCellValueFactory(new PropertyValueFactory<person, String>("gender"));
}
@FXML
private void handleCustomerAddNew(ActionEvent event) {
Parent root;
try {
root = FXMLLoader.load(getClass().getResource("customerinformation.fxml"));
Stage stage = new Stage();
stage.setTitle("Customer Information");
stage.setScene(new Scene(root, 400, 340));
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public void handleNewSubmit() {
System.out.println("beginning of handleNewSubmit");
ArrayList<person> registeredPersons=new ArrayList<person>();
Connection conn = main.getConn();
String query = "SELECT * FROM CUSTOMER";
try{
PreparedStatement stmt = conn.prepareStatement(query);
ResultSet resultset = stmt.executeQuery();
while(resultset.next()){
System.out.println(resultset.getString("FirstName")+" "+resultset.getString("LastName"));
registeredPersons.add(new person(resultset.getString("FirstName"),resultset.getString("LastName"), resultset.getString("StreetAddress"),
resultset.getString("City"),resultset.getString("State"),resultset.getString("Zipcode"),resultset.getString("Gender")));
}}
catch (Exception e){
e.printStackTrace();
}
ObservableList<person> items = FXCollections.observableArrayList(registeredPersons);
System.out.println("before adding to tView");
tView.setItems(items);
System.out.println("after adding to tView");
}
}
Ошибки:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8885)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3890)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1885)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2618)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.mac.MacView.notifyMouse(MacView.java:127)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1782)
... 44 more
Caused by: java.lang.NullPointerException
at retailsystem.RetailSystemController.handleNewSubmit(RetailSystemController.java:127)
at retailsystem.CustomerInformationController.handleSubmit(CustomerInformationController.java:102)
... 55 more