Javant Javant - 4 months ago 23
Java Question

How to fix this NullPointer in my ViewList Selected Item Action Listener?

I am attempting to write a listener for when a new item is selected on a ListView Node. But the problem is I get a NPE when I run the application. From looking at it I am assuming that this may be being thrown because the list is empty upon run time, but I have no clue how to fix it.

Update 1:
From further investigation, with a btn action listener I am writing. When I tried to access the selected item, I was thrown a null pointer exception. Work around was wrapping it with a try/catch, which ultimately fixed it. Will try something similar with viewList and see if it will work

Update 2:
try/catch(NPE) workaround worked for the viewList action Listener

Exception

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at com.formatter.Main.start(Main.java:186)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
... 1 more
Exception running application com.formatter.Main


Code at Line 186: ListView.getSelectionModel().selectedItemProperty().addListener(new...

listView = new ListView<BusinessCard>();
observableList = FXCollections.observableList(cardModel.getCards());
//cardModel.getCards() -> ArrayList<BusinessCards>

// ListView Listener, changes text fields for the selected B.C in ViewLsit
listView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<BusinessCard>() {
@Override
public void changed(ObservableValue<? extends BusinessCard> arg0, BusinessCard oldval,BusinessCard newVal) {
if(newVal != null) setDataFields(newVal.getUI());
}
});

Answer

When calling listView.getSelectionModel().selectedItemProperty()... and the view list was empty, null was being thrown. Which is said to be a behavior in the documentation

Workaround:

// ListView Listener, changes text fields for the selected B.C in ViewList

            listView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<BusinessCard>() {
                @Override
                public void changed(ObservableValue<? extends BusinessCard> arg0, BusinessCard oldval,BusinessCard newVal) {
                    if(newVal == null) return;
                    setDataFields(newVal.getUI());      
                }
            });