I have an “added” button which must add elements to my database, so if the fields concerned are empty it must display an alert saying that the fields are empty, but it shows me an error at the console level.
here is my code and my error message :
String name = TFName.getText();
String price = TFPrice.getText();
String quantity = TFQuantity.getText();
Alert alert;
if(name.isEmpty() || price.isEmpty() || quantity.isEmpty()) {
alert = new Alert(AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText(null);
alert.setContentText("Fill in all fields!!");
alert.showAndWait();
}else {
try {
Connection conn = Connection.connect();
String sql = "INSERT into product (name, unitPrice, quantity) value(?,?,?)";
PreparedStatement stmt = conn.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
stmt.setString(1, name);
stmt.setString(2, price);
stmt.setString(3, quantity);
stmt.executeUpdate();
// Get the generated ID of the new product
ResultSet generatedKeys = stmt.getGeneratedKeys();
int newId = -1;
if (generatedKeys.next()) {
newId = generatedKeys.getInt(1);
}
// Add the new product to the observable list
Product newProduct = new Product(newId, name, Integer.parseInt(price), Integer.parseInt(quantity));
data.add(newProduct);
conn.close();
TFName.setText(null);
TFPrice.setText(null);
TFQuantity.setText(null);
}catch (NumberFormatException e) {
alert = new Alert(AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText(null);
alert.setContentText("Price and quantity must be numbers!");
alert.showAndWait();
}catch (SQLException e) {
System.out.println("Error" + e.getMessage());
}
}
}
and here is the error message
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at javafx.fxml@22/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1863) at javafx.fxml@22/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1731) at javafx.base@22/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) at javafx.base@22/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:232) at javafx.base@22/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:189) at javafx.base@22/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at javafx.base@22/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at javafx.base@22/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at javafx.base@22/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at javafx.base@22/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at javafx.base@22/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at javafx.base@22/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at javafx.base@22/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at javafx.base@22/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) at javafx.base@22/javafx.event.Event.fireEvent(Event.java:198) at javafx.graphics@22/javafx.scene.Node.fireEvent(Node.java:8917) at javafx.controls@22/javafx.scene.control.Button.fire(Button.java:203) at javafx.controls@22/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:207) at javafx.controls@22/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274) at javafx.base@22/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247) at javafx.base@22/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) at javafx.base@22/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:232) at javafx.base@22/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:189) at javafx.base@22/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at javafx.base@22/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at javafx.base@22/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at javafx.base@22/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at javafx.base@22/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at javafx.base@22/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at javafx.base@22/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at javafx.base@22/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at javafx.base@22/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) at javafx.base@22/javafx.event.Event.fireEvent(Event.java:198) at javafx.graphics@22/javafx.scene.Scene$MouseHandler.process(Scene.java:3985) at javafx.graphics@22/javafx.scene.Scene.processMouseEvent(Scene.java:1891) at javafx.graphics@22/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2709) at javafx.graphics@22/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411) at javafx.graphics@22/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301) at java.base/java.security.AccessController.doPrivileged(AccessController.java:399) at javafx.graphics@22/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450) at javafx.graphics@22/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:430) at javafx.graphics@22/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449) at javafx.graphics@22/com.sun.glass.ui.View.handleMouseEvent(View.java:551) at javafx.graphics@22/com.sun.glass.ui.View.notifyMouse(View.java:937) at javafx.graphics@22/com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at javafx.graphics@22/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184) at java.base/java.lang.Thread.run(Thread.java:833) 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:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:72) 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:568) at javafx.base@22/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:270) at javafx.fxml@22/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84) at javafx.fxml@22/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1858) ... 46 more Caused by: java.lang.NullPointerException: Cannot invoke "String.isEmpty()" because "name" is null at GestionDeBoutique/controller.GestionProduitController.ajouterProduit(GestionProduitController.java:72) ... 57 more
I hope I will have a solution to my problem
New contributor
Abdoulaye Cissé is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.