I was trying to save a task on this JAVA project connected to sql, but this error appeared when i was testing it on my main. That's what i asked the file to do:
TaskController taskController = new TaskController();
Task task = new Task();
task.setIdProject(2);
task.setName("Criar Interface Gráfica");
task.setDescription("Criar uma GUI para o projeto JAVA ");
taskController.save(task);
and i got this error:
Exception in thread "main" java.lang.RuntimeException: Erro ao salvar a tarefa
at controller.TaskController.save(TaskController.java:49)
at TodoApp.App.main(App.java:27)
Caused by: java.lang.NullPointerException
at controller.TaskController.save(TaskController.java:44)
... 1 more
that's my TaskController:
public void save( Task task){
String sql = "INSERT INTO tasks(IdProject,name,description,status,notes,deadline,createdAt, updatedAt) VALUES (?,?,?,?,?,?,?,?)";
Connection conn = null;
PreparedStatement statement = null;
try {
conn = ConnectionFactory.getConnection();
statement = conn.prepareStatement(sql);
statement.setInt(1, task.getIdProject());
statement.setString(2,task.getName());
statement.setString(3, task.getDescription());
statement.setBoolean(4, task.isStatus());
statement.setString(5, task.getNotes());
statement.setDate(6, new Date(task.getDeadline().getTime()));
statement.setDate(7, new Date(task.getCreatedAt().getTime()));
statement.setDate(8, new Date(task.getUpdatedAt().getTime()));
statement.execute();
} catch (Exception ex) {
throw new RuntimeException("Erro ao salvar a tarefa"
+ ex.toString());
} finally {
ConnectionFactory.closeConnection(conn, statement);
}
}
I was trying to save a new task in the MySQL databank by my JAVA file it resulted in a error called java.lang.NullPointerException