0

I'm trying to use setItems method of ListView but it gives me NullPointerException. Error seems to be triggered by the line 62 of the FXMLController.java. I read data from .xml file.

I've tried debugging, checking fxid, printing out the values of the arraylist and the observablelist and reading other people's posts.

ParcoAutonoleggioJavaFX.java (main)


import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class ParcoAutonoleggioJavaFX extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXML.fxml"));
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
        FXMLController fXMLController = new FXMLController();
        fXMLController.aggiornaLista();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

FXMLController.java

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.ResourceBundle;
import javafx.beans.InvalidationListener;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.fxml.Initializable;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javafx.scene.control.ListView;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class FXMLController implements Initializable {

    @FXML
    ListView list;

    @Override
    public void initialize(URL url, ResourceBundle rb) {

    }

    @FXML
    public void aggiornaLista() throws ParserConfigurationException, SAXException, IOException {
        File fXmlFile = new File("parcoAutonoleggio.xml");
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(fXmlFile);
        document.getDocumentElement().normalize();
        NodeList nodeList = document.getElementsByTagName("auto");
        ArrayList<String> arrayList = new ArrayList<>();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            Element element = (Element) node;
            arrayList.add(element.getElementsByTagName("modello").item(0).getTextContent());
        }
        ObservableList<String> observableList = FXCollections.observableArrayList(arrayList);
        list.setItems(observableList);
    }
}

FXML.fxml


<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="480.0" prefWidth="640.0" xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="parcoautonoleggiojavafx.FXMLController">
    <children>
      <ListView fx:id="list" layoutX="220.0" layoutY="140.0" prefHeight="200.0" prefWidth="200.0" />
    </children>
</AnchorPane>

parcoAutonoleggio.xml

<parco>
    <!-- -->
    <auto>
        <codice>1</codice>
        <modello>fiat 600 1999</modello>
        <noleggiato>true</noleggiato>
        <giorniNoleggio>30</giorniNoleggio>
    </auto>
    <auto>
        <codice>2</codice>
        <modello>fiat 500 2012</modello>
        <noleggiato>true</noleggiato>
        <giorniNoleggio>180</giorniNoleggio>
    </auto>
    <auto>
        <codice>3</codice>
        <modello>ford raptor 2018</modello>
        <noleggiato>true</noleggiato>
        <giorniNoleggio>730</giorniNoleggio>
    </auto>
    <auto>
        <codice>4</codice>
        <modello>ford focus 2007</modello>
        <noleggiato>true</noleggiato>
        <giorniNoleggio>90</giorniNoleggio>
    </auto>
    <auto>
        <codice>5</codice>
        <modello>toyota aygo 2006</modello>
        <noleggiato>false</noleggiato>
        <giorniNoleggio>0</giorniNoleggio>
    </auto>
    <auto>
        <codice>6</codice>
        <modello>toyota lotus 2008</modello>
        <noleggiato>false</noleggiato>
        <giorniNoleggio>0</giorniNoleggio>
    </auto>
    <auto>
        <codice>7</codice>
        <modello>dodge viper 2003</modello>
        <noleggiato>true</noleggiato>
        <giorniNoleggio>60</giorniNoleggio>
    </auto>
    <auto>
        <codice>8</codice>
        <modello>mitsubishi lancer evo x</modello>
        <noleggiato>false</noleggiato>
        <giorniNoleggio>0</giorniNoleggio>
    </auto>
</parco>

Stack trace

java.lang.reflect.InvocationTargetException
    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:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    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:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:873)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException: 
/F:/TPS/JAVA%20XML/parcoAutonoleggioJavaFX/build/classes/parcoautonoleggiojavafx/FXML.fxml:8

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:922)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
    at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at parcoautonoleggiojavafx.ParcoAutonoleggioJavaFX.start(ParcoAutonoleggioJavaFX.java:13)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    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$4(WinApplication.java:185)
    ... 1 more
Caused by: java.lang.ClassNotFoundException: parcoautonoleggiojavafx.FXMLController
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:920)
    ... 22 more
Exception running application parcoautonoleggiojavafx.ParcoAutonoleggioJavaFX
C:\Users\rgrrlv02a31z154g\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)```
  • please add the complete stack trace (formatted as code) and in your code indicate the line that throws the NPE – kleopatra Apr 12 '22 at 11:39
  • ahh .. I see what's wrong: you must __load__ the controller (vs. instantiating it) – kleopatra Apr 12 '22 at 11:40
  • You are not calling `aggiornaLista()` on the controller. The `list` field will only be initialized in the actual controller instance created by the `FXMLLoader`. – James_D Apr 12 '22 at 11:46
  • It's also unclear why you want to do this. Why not just call `aggiornaLista()` from `initialize()`? – James_D Apr 12 '22 at 12:56

0 Answers0