-1

I am trying to add some values to a TableView, but even thought the ObservableList obsEvts updates and has the values it should have, they are not correctly assigned to the TableView.

I tryed a lot of things, saw a lot of videos, tutorials, questions here, none of them solved my problem.

My whole code it right here: https://github.com/Skwead/Agenda/tree/bugs My problem is in the Controller class only, in these lines: https://github.com/Skwead/Agenda/blob/efbb594e1ce77fb1e721bc90b0429e719ec87aef/src/sample/Controller.java#L61

public class Controller implements Initializable{
@FXML private TableView<SkEvent> todayTable = new TableView<>();
    @FXML private TableColumn<SkEvent, Date> horaTodoCol = new TableColumn<>();
    @FXML private TableColumn<SkEvent, String> estadoTodoCol = new TableColumn<>();
    @FXML private TableColumn<SkEvent, String> tarefaTodoCol = new TableColumn<>();

    public void setupEvts(){
ObservableList<SkEvent> obsEvts = FXCollections.observableArrayList(calendarHandler.getToday());

        horaTodoCol.setCellValueFactory(new PropertyValueFactory<SkEvent, Date>("date"));
        tarefaTodoCol.setCellValueFactory(new PropertyValueFactory<SkEvent, String>("name"));

        todayTable.setItems(obsEvts);

        todayTable.getColumns().addAll(horaTodoCol, estadoTodoCol ,tarefaTodoCol);
        todayTable.getColumns().set(0, horaTodoCol);
        todayTable.getColumns().set(2, tarefaTodoCol);
    }

}

public class CalendarHandler {

    private List<SkEvent> sortedEvts = new ArrayList<>();

    public ArrayList<SkEvent> getToday(){
        ArrayList<SkEvent> todaySchedule = new ArrayList<>();

        for (SkEvent event : sortedEvts) {
            if (event.getDate().getDay() == (new Date(LocalDateTime.now().getYear(), LocalDateTime.now().getMonthValue(),
                    LocalDateTime.now().getDayOfMonth()).getDay())) {
                todaySchedule.add(event);
            }
        }

        Collections.sort(todaySchedule, Comparator.comparing(SkEvent::getDate)); 
        return todaySchedule;
    }

No error is shown, at the end of this method call the todatTable should have all the data in obsEvts.

Skwead
  • 41
  • 5
  • 2
    We require a [mcve] to be included in the question text itself. Links are insufficient. BTW: A look at the `CalendarHandler` class shows that you don't properly convert between `java.util.Date` and `LocalDateTime` (not sure why you don't use `LocalDate` or why you use `java.util.Date` at all). – fabian Aug 03 '19 at 21:53
  • I am sorry, I thought what I gave was enought, I have added averything needed now. Anyways, I need at least hours and minutes, wich LocalDate cannot handle, only Date and LocalDateTime. – Skwead Aug 04 '19 at 07:14
  • You didn't; There is no element with `fx:id="todayTable"` in the fxml file, so you fill a table that is never added to a scene. BTW: your use of `static` has me concerned: `Controller.getC` returns a instance that is (probably) never used with a fxml. Rather than using `static` data to retrieve data I recommend creating an observable model and passing it around. As for the date conversions: print the results. today it should print september 3919; additional nasty surprises on the 31st and in december. Use `Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant())` instead... – fabian Aug 04 '19 at 08:18
  • I do have a `fx;id="todayTable"`, it is right here https://github.com/Skwead/Agenda/blob/efbb594e1ce77fb1e721bc90b0429e719ec87aef/src/sample/resources/sample.fxml#L17. I will take a look on the observable model, I used static to try to get data from a controller to another, since they cannot be instanciated and I do not want to go into Spring orother frameworks right now. I will change the way I work with Dates as soon as I get this fixed, so I can test it properly, many thanks for the help – Skwead Aug 04 '19 at 09:08

2 Answers2

2

Without testing your code, I would say that your mistake is in your controller, where you are creating new table columns, instead of using the one that are in your fxml file...

 @FXML private TableView<SkEvent> calendarTable = new TableView<>();
@FXML private TableColumn<SkEvent, Date> horaCol = new TableColumn<>();
@FXML private TableColumn<SkEvent, String> segCol = new TableColumn<>();
@FXML private TableColumn<SkEvent, String> terCol = new TableColumn<>();
@FXML private TableColumn<SkEvent, String> quaCol = new TableColumn<>();
@FXML private TableColumn<SkEvent, String> quiCol = new TableColumn<>();
@FXML private TableColumn<SkEvent, String> sexCol = new TableColumn<>();
@FXML private TableColumn<SkEvent, String> sabCol = new TableColumn<>();
@FXML private TableColumn<SkEvent, String> domCol = new TableColumn<>();
......
.....

this should be like this:

 @FXML private TableView<SkEvent> calendarTable;
@FXML private TableColumn<SkEvent, Date> horaCol;
@FXML private TableColumn<SkEvent, String> segCol;
@FXML private TableColumn<SkEvent, String> terCol;
@FXML private TableColumn<SkEvent, String> quaCol;
@FXML private TableColumn<SkEvent, String> quiCol;
@FXML private TableColumn<SkEvent, String> sexCol;
@FXML private TableColumn<SkEvent, String> sabCol;
@FXML private TableColumn<SkEvent, String> domCol;

Are you using SceneBuilder? If so, just give thouse fxml column names to your columns in SceneBuilder.

smithnblack
  • 505
  • 7
  • 17
  • If I do as you say I get a null pointer exception at Controller line 65, the column is null. Here's the complete npe error: ```java Caused by: java.lang.NullPointerException at Agenda/sample.Controller.setupEvts(Controller.java:65) at Agenda/sample.calendar.CallendarController.validateInput(CallendarController.java:50) at Agenda/sample.calendar.CallendarController.click(CallendarController.java:30)``` – Skwead Aug 04 '19 at 07:23
  • @Skwead then track the NPE .. instantiating fields that are injected by the fxml is _wrong_ just as smithnblack explained in this answer .. – kleopatra Aug 04 '19 at 11:36
  • Hello, could you provide us with your fxml File? My guess is that you did not use fxml annotation properly in your fxml file and thats why you got NPE in your controller. As I stated in my first answer, just instantiate your columns in your fxml and use it in your controller... in your fxml file for every table column you shoud have something like this" fx:id="horaCol", etc. Look here: https://stackoverflow.com/a/30213067/2976876 – smithnblack Aug 04 '19 at 14:20
  • @kleopatra I did track it, as I said, all the columns and tables are null, as soon as I try to use them the npe is thrown. The fxml file is right here https://github.com/Skwead/Agenda/blob/efbb594e1ce77fb1e721bc90b0429e719ec87aef/src/sample/resources/sample.fxml – Skwead Aug 05 '19 at 07:18
  • well, then we have different meanings for track ;) The driving force must be to make your code work, right? The error you describe in your question (nothing visible) is introduced by instantiating fields that are injected via fxml: the injection overwrites the previously instantiated fields - that's what this answer correctly states. Repeating: it's __WRONG__ to instantiate injected field!!! Your _if I don't instantiate I get an NPE_ - simply means that something goes wrong with the injection (should work), which you have to find (vs evading by instantiating the fields) – kleopatra Aug 06 '19 at 08:32
  • Alright, thanks for the advice concerning the code I should post. Regarding the problem itself, it seems then I should get back to the npe problem, since the solution I found is not the right one. Then, how would I solve it? As I explained in a comment to an answer here the column is not part of the table (since the debugger tells `horaTodoCol.getTableView().toString()` is null). This seems to be the big problem – Skwead Aug 06 '19 at 09:04
0

you are also instantiating your tableview for the second time in the line 30 in your controller:

@FXML private TableView<SkEvent> todayTable = new TableView<>();

and also adding your columns to that 2nd tableView todayTable that are already instantiated in your fxml here:

todayTable.setItems(obsEvts);
todayTable.getColumns().addAll(horaTodoCol, estadoTodoCol ,tarefaTodoCol);
todayTable.getColumns().set(0, horaTodoCol);
todayTable.getColumns().set(2, tarefaTodoCol);

so, my suggestion would be: remove all from your controller with new TableView<>() and new TableColumn<>() constructor calls and also this 3 lines in your code:

todayTable.getColumns().addAll(horaTodoCol, estadoTodoCol ,tarefaTodoCol);
todayTable.getColumns().set(0, horaTodoCol);
todayTable.getColumns().set(2, tarefaTodoCol);

Let me know if this helps!

smithnblack
  • 505
  • 7
  • 17
  • I tried it, but removing the instanciations gives me the npe I already mentioned. I have 2 tables in the scene, todayTable and calendarTable are different, I've tryed to coment the other table and columns out, but nothing changed. I removed those 3 lines too, nothing changed. – Skwead Aug 06 '19 at 08:10
  • Sorry for writing two comments in such a short period of time, but the previous one can no longer be edited by me. I think I might have figured out what the problem realy is, althought I still can't figure out how to solve it. `horaTodoCol.getTableView().toString()` throws a npe. Seems the columns are not assigned to the table, even thought they are in scene builder. I don't have that line on my code, I used it on the debugger to see if the column was ound to the table – Skwead Aug 06 '19 at 08:17
  • please don't post 2 answers, edit the first and add more details as needed – kleopatra Aug 06 '19 at 08:34