-1

I am using tableview, from javafx, and in some moments of my code, there will be element that will be null, how to treat it? Sometimes idProcesso and tempoE will be null, but, i want to show the idProcessador.

each line contains the variables of the Processor class, but one of these variables is another object, Process, and this in turn can be null sometimes, how to modify so that the lines show nothing? because when I ask to show will give a nullpoint

public void initialize(URL location, ResourceBundle resources) {

    idProcessador = new TableColumn("ID Processador");
    idProcessador.setPrefWidth(100);
    idProcessador.setCellValueFactory(data -> 
            new SimpleIntegerProperty(data.getValue().getId()).asObject());
    idProcesso = new TableColumn("ID Processo");
    idProcesso.setCellValueFactory(data -> 
            new SimpleIntegerProperty(data.getValue().
                        getProcessoRodando().getId()).asObject());
    tempoE = new TableColumn("Tempo Execução Restante");
    tempoE.setPrefWidth(100);
    tempoE.setCellValueFactory(data -> 
            new SimpleIntegerProperty(data.getValue().
                        getProcessoRodando().getTempoExecucaoRest()).asObject());
    tabProcessador.getColumns().addAll(idProcessador, idProcesso,tempoE);
}



public class Processador {
    private Processo processoRodando;
    private int id;
    private Integer quantum =  null;    
}




public class Processo implements Comparable<Processo> {
    int id ;
    int tempoExecucao;
    int estadoProcesso;
    int tempoExecucaoRest;
}
Yuki Kato
  • 13
  • 4
  • 1
    “Sometimes idProcesso and tempoE will be null” —They cannot be null, since you have assigned them with `new TableColumn` and in Java `new` will never return null. Did you mean that their those columns sometimes have null cell values in some rows? – VGR Sep 28 '19 at 02:15
  • It's not exactly clear what you're having trouble with. A `TableCell` is perfectly capable of holding a `null` item and how that is displayed is fully customizable by providing a custom `TableColumn#cellFactory` that returns a custom `TableCell` implementation that overrides [`Cell#updateItem(T,boolean)`](https://openjfx.io/javadoc/13/javafx.controls/javafx/scene/control/Cell.html#updateItem(T,boolean)). Look at the documentation to see an example of how to override that method. – Slaw Sep 28 '19 at 06:06
  • @VGR for example, each row contains the variables of the Processor class, but one of these variables is another object, Process, and this in turn can be null sometimes, how to modify so that the lines show nothing? because when I ask to show will give a nullpoint – Yuki Kato Sep 29 '19 at 01:30
  • Is `getProcessoRodando()` returning null for those rows? – VGR Sep 30 '19 at 02:30
  • @VGR sometimes , yes – Yuki Kato Sep 30 '19 at 05:12

1 Answers1

1

It appears your problem is these two lines:

idProcesso.setCellValueFactory(data -> 
        new SimpleIntegerProperty(data.getValue().
                    getProcessoRodando().getId()).asObject());

tempoE.setCellValueFactory(data -> 
        new SimpleIntegerProperty(data.getValue().
                    getProcessoRodando().getTempoExecucaoRest()).asObject());

Sometimes data.getValue().getProcessoRodando() returns null. Obviously, you cannot call a method on a null value.

The easiest solution is to take the advice given in What is a NullPointerException, and how do I fix it? and avoid calling a method on a null value:

idProcesso.setCellValueFactory(data -> {
        Processo processo = data.getValue().getProcessoRodando();
        return new SimpleObjectProperty<Integer>(processo != null ?
            Integer.valueOf(processo.getId()) : null);
    });

tempoE.setCellValueFactory(data -> {
        Processo processo = data.getValue().getProcessoRodando();
        return new SimpleObjectProperty<Integer>(processo != null ?
            Integer.valueOf(processo.getTempoExecucaoRest()) : null);
    });

You could also use Bindings.select or Bindings.selectString, but they’re unreliable as they use reflection, which means if you make a spelling error, the compiler won’t notice or flag it.

As an additional note, you should enable all compiler warnings and address them. The compiler will then warn you that your TableColumn objects should have types:

private TableColumn<Processador, Integer> idProcessador;

// ...

idProcessador = new TableColumn<>("ID Processador");
VGR
  • 40,506
  • 4
  • 48
  • 63