0

I am trying to dynamically populate a ListView with strings gathered from a website that are within certain tags. The following code uses Apache Commons lang and Apache Commons IO to firstly go to the website and then use a for loop to put the text within the certain tags into the ListView each iteration. But I get a java.lang.NullPointerException on the second line in the for loop.

JavaFX source code:

@FXML
public ListView<String> applicationList;    
public ObservableList<String> applicationListItems;

try {

    URL appListFileURL = new URL("http://example.com");
    String appListFileDownload = IOUtils.toString(appListFileURL.openStream());

    for (int i = 0; i < 5; i++) {

        String applicationListItem = StringUtils.substringBetween(appListFileDownload, "[Application" + i + "]", "[/Application" + i + "]");
        applicationListItems.add(applicationListItem); <----- Exception origin

    }

} catch (Exception e) { 

        e.printStackTrace();

}

applicationList.setItems(applicationListItems);

The websites source code:

[Application0]Banana[/Application0]
[Application1]Apple[/Application1]
[Application2]Orange[/Application2]
[Application3]Pear[/Application3]

I have commented out applicationListItems.add(applicationListItem); and replaced it with System.out.println(applicationListItem); and it returns what would be expected: Banana Apple Orange Pear

But I don't understand how I am getting a null pointer exception when the console didn't return null. Does any one know why this is happening?

Note: All links from the FXML file and the controller class are working because this is the only component not working.

Stack trace:

java.lang.NullPointerException
at com.nementia.updater.UpdaterController.actionListSelected(UpdaterController.java:92)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
ayeama
  • 173
  • 1
  • 2
  • 11
  • `applicationListItems` (the list) is null. – James_D Dec 16 '15 at 10:17
  • @James_D Thank you James I added `applicationListItems = FXCollections.observableArrayList();` in the initialize method of the controller class and it worked. – ayeama Dec 16 '15 at 10:45

0 Answers0