1
votes

I have a program in JAVAFX which contains a list of news. I color each new by its severity and add it to the ListView.

When I add a new to the list, I receive a NullPointerException.

After Debugging I've noticed that the 'item' object which I get in the updateItem() method as a parameter is null. Couldn't figure out why.

Here is some of my code:

public void initializeListView(IntelligenceSource source){
    newsListData =FXCollections.observableArrayList ();

    for (IntelligenceNew news : source.getListOfNews())
        newsListData.add(news);

    newsList.setItems(newsListData);
    newsList.setCellFactory(e -> new ListViewFormat());
}

Inner class for the ListView format:

public class ListViewFormat extends ListCell<IntelligenceNew> {
    @Override
    protected void updateItem(IntelligenceNew item, boolean empty) {
        super.updateItem(item, empty);
        setText(item.getName());

        switch (item.getSeverity()){
            case 1: setTextFill(Color.GREEN);
                break;
            case 2: setTextFill(Color.ORANGE);
                break;
            case 3: setTextFill(Color.RED);
        }
    }
}

Every time a new added to a source, I activate the method InitializeListView with the appropriate source.

The exception message:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at GUI.MainWindowController$ListViewFormat.updateItem(MainWindowController.java:201)
at GUI.MainWindowController$ListViewFormat.updateItem(MainWindowController.java:197)
at javafx.scene.control.ListCell.updateItem(ListCell.java:480)
at javafx.scene.control.ListCell.access$300(ListCell.java:72)
at javafx.scene.control.ListCell$4.invalidated(ListCell.java:299)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:111)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
at javafx.scene.control.ListCell.setListView(ListCell.java:305)
at javafx.scene.control.ListCell.updateListView(ListCell.java:494)
at com.sun.javafx.scene.control.skin.ListViewSkin.createCell(ListViewSkin.java:267)
at com.sun.javafx.scene.control.skin.ListViewSkin.lambda$new$369(ListViewSkin.java:89)
at com.sun.javafx.scene.control.skin.ListViewSkin$$Lambda$189/183721512.call(Unknown Source)
at com.sun.javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1771)
at com.sun.javafx.scene.control.skin.VirtualFlow.getCellLength(VirtualFlow.java:1872)
at com.sun.javafx.scene.control.skin.VirtualFlow.computeViewportOffset(VirtualFlow.java:2511)
at com.sun.javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1189)
at javafx.scene.Parent.layout(Parent.java:1076)
at javafx.scene.Parent.layout(Parent.java:1082)
at javafx.scene.Parent.layout(Parent.java:1082)
at javafx.scene.Parent.layout(Parent.java:1082)
at javafx.scene.Parent.layout(Parent.java:1082)
at javafx.scene.Parent.layout(Parent.java:1082)
at javafx.scene.Scene.doLayoutPass(Scene.java:552)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2397)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$30(Toolkit.java:314)
at com.sun.javafx.tk.Toolkit$$Lambda$275/981231226.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:313)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:340)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:525)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:505)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$400(QuantumToolkit.java:334)
at com.sun.javafx.tk.quantum.QuantumToolkit$$Lambda$42/663212973.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$38/1882560110.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
1
Items are null for the rows which doesn't have any data. Just add a null check against item before using setText(item.getName()); - ItachiUchiha
Thank you so much for your help! It works like a charm. - O.Kar

1 Answers

1
votes

You'll have to manage a Situation like what if null values are passed.
In that case you'll have to alter your code like below.

 public class ListViewFormat extends ListCell<IntelligenceNew> {
@Override
protected void updateItem(IntelligenceNew item, boolean empty) {
    super.updateItem(item, empty);
      if(item==null)
      {
         setText(null);
      }
     else
      {
    setText(item.getName());

    switch (item.getSeverity())
      {
        case 1: setTextFill(Color.GREEN);
            break;
        case 2: setTextFill(Color.ORANGE);
            break;
        case 3: setTextFill(Color.RED);
      }
     } //else ends
}
   }

This Worked for Me,I too faced this problem.