Desculpa!
StackTrace
Exception in thread “main” java.lang.RuntimeException:
Erro ao salvar a tarefa null
at controller.TaskController.save(TaskController.java:60)
at todoApp.Main.main(Main.java:49)
Caused by: java.lang.NullPointerException
at controller.TaskController.save(TaskController.java:53)
Código fonte:
Main.java{
TaskController taskController = new TaskController();
Task task = new Task();
task.setName("Segunda tarefa");
task.setDescription("Segunda descrição ");
task.setIdProject(9);
taskController.save(task);
}
public class TaskController {
public void save(Task task) {
String sql = "INSERT INTO tasks(idProject ,"
+ "name, "
+ "description, "
+ "status, "
+ "notes, "
+ "completed, "
+ "deadline, "
+ "createdAt, "
+ "updatedAt) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?,?)";
Connection connection = null;
PreparedStatement statement = null;
try {
connection = ConnectionFactory.getConnection();
statement = connection.prepareStatement(sql);
statement.setInt(1, task.getIdProject());
statement.setString(2, task.getName());
statement.setString(3, task.getDescription());
statement.setByte(4, task.getStatus());
statement.setString(5, task.getNotes());
statement.setBoolean(6, task.isCompleted());
statement.setDate(7, new java.sql.Date(task.getDeadline().getTime()));
statement.setDate(8, new java.sql.Date(task.getCreatedAt().getTime()));
statement.setDate(9, new java.sql.Date(task.getUpdatedAt().getTime()));
statement.execute();
} catch (Exception ex) {
throw new RuntimeException("Erro ao salvar a tarefa " + ex.getMessage(), ex);
}
}
public class Task {
private int id;
private int idProject ;
private String name;
private String description;
private byte status;
private String notes;
private Date deadline;
private boolean completed;
private Date createdAt;
private Date updatedAt;
public Task(int id, int idProject, String name, String description, byte status, String notes, Date deadline, boolean completed, Date createdAt, Date updatedAt) {
this.id = id;
this.idProject = idProject;
this.name = name;
this.description = description;
this.status = status;
this.notes = notes;
this.deadline = deadline;
this.completed = completed;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}
public Task(){
this.completed = false;
this.createdAt = new Date();
this.updatedAt = new Date();
}