0

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

  • This is bad: `throw new RuntimeException("Erro ao salvar a tarefa" + ex.toString());`. Do `throw new RuntimeException("Erro ao salvar a tarefa", ex)` to preserve the context – Michael Jan 17 '23 at 18:36
  • Line 44. We don't know which one that is. – Michael Jan 17 '23 at 18:40
  • Line 44 is : statement.setDate(6, new Date(task.getDeadline().getTime())); – Julian Medeiros Jan 17 '23 at 18:41
  • Then `task` doesn't have a deadline set on it. Set one, or wrap that statement in a `if` statement which checks whether or not there's a deadline. You can't call `.getTime()` on nothing. – Michael Jan 17 '23 at 18:42
  • I'm sorry i think i didn't understand I have a "private Date deadline;" on my Task.Java and then a constructor and a getter and setter for deadline. I also have a Date "deadline" set on my MySql DB Where exactly should i set the deadline? Also, how should i do that if statement? I'm a bit confused. – Julian Medeiros Jan 17 '23 at 18:49
  • I strongly recommend you don’t use `Date`. That class is poorly designed and long outdated. Instead use for example `Instant` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jan 17 '23 at 18:53

0 Answers0