So, Im fairly new to Java and SQL. I had an assignment to add values from a JTextfield into an SQL Database, So everything is working but I get this error at the insert statement.:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "org.ibex.nestedvm.Runtime.xmalloc(int)" because "this.rt" is null
This is my Code:
public class sqlDataBase {
private static final String CREATE_TABLE_EMP="CREATE TABLE Employees ("
+ "ID INT NOT NULL,"
+ "Name VARCHAR(45) NOT NULL,"
+ "Job VARCHAR(45) NOT NULL,"
+ "Salary INT NOT NULL)";
Connection conn = null;
Statement stmt = null;
public void connectDB()
{
try{
Class.forName("org.sqlite.JDBC");
conn =DriverManager.getConnection("jdbc:sqlite:D:\\OneDrive - Higher Education Commission\\NetBeansProjects\\SalesManagementSystem\\empnet.sqlite");
stmt = (Statement) conn.createStatement();
JOptionPane.showMessageDialog(null, "Connection to database is successful");
stmt.execute(CREATE_TABLE_EMP);
System.out.println("Table created");
}catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(sqlDataBase.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void addEmployee(String name, String job, int id, int salary) throws SQLException
{
connectDB();
String sql = "INSERT INTO Employees" + "VALUES ('"+id+"', '"+name+"', '"+job+"', '"+salary+"')"; //error in this line
stmt.executeUpdate(sql);
}
}
This is my AddButton:
private void AddEmployeeButtonActionPerformed(java.awt.event.ActionEvent evt) {
String name, job;
int salary, id;
name = (AddEmployeeName_TextField.getText());
job = (AddEmployeeJob_TextField.getText());
salary=Integer.parseInt(AddEmployeeSalary_TextField.getText());
id=Integer.parseInt(AddEmployeeID_TextField.getText());
sqlDataBase update = new sqlDataBase();
try {
update.addEmployee(name,job,id,salary); //this button is in different class
} catch (SQLException ex) {
Logger.getLogger(AddEmployee.class.getName()).log(Level.SEVERE, null, ex);
}
}
These are my imports for sql:
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
The table creates successfully and the database connects too I have been trying for quite a while to figure out what's going on but unable to solve