I am trying to create a Java application using Netbeans 8.2 for a uni project. I'm having an issue after creating a JFrame
for adding some data to the database.
NetBeans highlights the final statement ujc.create(u2);
in the code segment below with the following error message:
Unreported exception PreexistingEntityException; must be caught or declared to be thrown
Can someone advise what I need to do to fix this issue? I have no idea what I'm doing wrong.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
Users u2 = new Users();
u2.setEmployeeno(jTextField1.getText());
u2.setFirstname(jTextField2.getText());
u2.setSurname(jTextField3.getText());
EntityManagerFactory emf = Persistence.createEntityManagerFactory(
"BatteryManagementSystemPU");
UsersJpaController ujc = new UsersJpaController(emf);
ujc.create(u2);
}
The
create(...)
method call here can fail, so we need to allow for that failure by surrounding the method with a try/catch block:You can find great information on the topic at the official tutorial page: https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html
The other option is to throw the error, however, the calling method for
jButton1ActionPerformed
will now need to catch the error instead, so it's better not to use this solution in most cases if you are unsure on what is happening:You can find more information on throwing an error here: https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html