0

I am fairly new to JavaFX and I am dealing with a problem. My program is to run a JavaFX program that uses a CSV to find what emojis from a mobile app game called Disney Emoji Blitz would meet certain criteria. I am also using Gradle as an automation tool for this program. It runs fine on Eclipse but when I use the zip distribution and run it in the bin folder (along with putting in the CSV), the program runs fine but when you click the compute button, the following message appears at runtime:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
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$MouseHandler.process(Scene.java:3862)
at javafx.scene.Scene.processMouseEvent(Scene.java:1849)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2590)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
at com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at com.sun.glass.ui.View.notifyMouse(View.java:942)
at com.sun.glass.ui.mac.MacView.notifyMouse(MacView.java:127)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
at com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1784)
... 26 more
Caused by: java.lang.NullPointerException
at FX.Controller.categoryExecution(Controller.java:166)
at FX.Controller.setUpComputation(Controller.java:123)
at FX.Controller.computeButtonPressed(Controller.java:112)
... 38 more

Here are my classes: Inside src/main/java:

Main.java

package FX;
    public class Main {
    public static void main(String[] args) {
        MainScreen.execute(args);
    }
}

MainScreen.java

package FX;

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

public class MainScreen extends Application {

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("DisneyEBFX.fxml"));
        Parent root = loader.load();
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Disney Emoji Blitz Categories");
        primaryStage.show();
    }
}

Controller.java

package FX;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;

import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListView;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Controller {

    @FXML
    private ComboBox<String> cat1;
    @FXML
    private ComboBox<String> cat2;
    @FXML
    private ComboBox<String> cat3;
    @FXML
    private ComboBox<String> box;
    @FXML
    private Button compute;

    @FXML
    private ListView<String> resultList;

    private final ArrayList<String> displayCats = new ArrayList<>(Arrays.asList("None", "Aladdin",
            "Alice in Wonderland", "Animal", "Bearded", "Beauty and the Beast", "Big Eared", "Blue", "Boys",
            "Cat or Dog", "Coco", "Fairies", "Finding Nemo/Dory", "Flying", "Four-legged", "Frozen", "Girls", "Green",
            "Hat-wearing", "Haunted Mansion", "Hercules", "Inside Out", "Jewelry", "Mickey and Friends", "Moana",
            "Monsters Inc.", "Mulan", "Nightmare Before Christmas", "Orange", "Outer Space", "Pirates of the Caribbean",
            "Pixar", "Pocahontas", "Prince", "Princess and the Frog", "Princesses", "Purple", "Red", "Rescue Rangers",
            "Robin Hood", "Scary", "Sleeping Beauty", "Snow White", "Star Wars", "Tangled", "The Emperor's New Groove",
            "The Incredibles", "The Jungle Book", "The Lion King", "The Little Mermaid", "The Muppets", "Toy Story",
            "Underwater", "Up", "Winnie the Pooh", "Wreck-It Ralph", "Yellow", "Zootopia"));
    private final String[] boxes = { "None", "Gold", "Rainbow", "Series", "Silver", "Villain" };

    private ArrayList<String> results = new ArrayList<>();

    BufferedReader br = null;

    // To run on start up
    @FXML
    public void initialize() {
        cat1.getItems().addAll(displayCats);
        cat1.getSelectionModel().selectFirst();
        cat2.getItems().addAll(displayCats);
        cat2.getSelectionModel().selectFirst();
        cat3.getItems().addAll(displayCats);
        cat3.getSelectionModel().selectFirst();
        box.getItems().addAll(boxes);
        box.getSelectionModel().selectFirst();

        compute.setDisable(true);

        cat1.getSelectionModel().selectedItemProperty()
                .addListener((ObservableValue<? extends String> observable, String oldVal, String newVal) -> {
                    computeButtonSetting();
                });
        cat2.getSelectionModel().selectedItemProperty()
                .addListener((ObservableValue<? extends String> observable, String oldVal, String newVal) -> {
                    computeButtonSetting();
                });
        cat3.getSelectionModel().selectedItemProperty()
                .addListener((ObservableValue<? extends String> observable, String oldVal, String newVal) -> {
                    computeButtonSetting();
                });
        box.getSelectionModel().selectedItemProperty()
                .addListener((ObservableValue<? extends String> observable, String oldVal, String newVal) -> {
                    computeButtonSetting();
                });
    }

    private void computeButtonSetting() {
        String s1 = cat1.getSelectionModel().getSelectedItem();
        String s2 = cat2.getSelectionModel().getSelectedItem();
        String s3 = cat3.getSelectionModel().getSelectedItem();
        String sBox = box.getSelectionModel().getSelectedItem();
        if (!s1.equals("None") && !s2.equals("None") && !s3.equals("None") && !sBox.equals("None")) {
            compute.setDisable(true);
        } else if (s1.equals("None") && s2.equals("None") && s3.equals("None") && sBox.equals("None")) {
            compute.setDisable(true);
        } else {
            compute.setDisable(false);
        }

    }

    @FXML
    public void computeButtonPressed() {
        String s1 = cat1.getSelectionModel().getSelectedItem();
        String s2 = cat2.getSelectionModel().getSelectedItem();
        String s3 = cat3.getSelectionModel().getSelectedItem();
        String sBox = box.getSelectionModel().getSelectedItem();
        HashSet<String> catsToCompute = new HashSet<>();
        catsToCompute.add(s1);
        catsToCompute.add(s2);
        catsToCompute.add(s3);
        if (catsToCompute.contains("None")) {
            catsToCompute.remove("None");
        }
        setUpComputation(catsToCompute, sBox);

    }

    private void setUpComputation(HashSet<String> cats, String box) {
        openFile();
        results.clear();
        resultList.getItems().clear();
        Object[] catsArray = cats.toArray();
        if (box.equals("None")) {
            if (catsArray.length == 1) {
                categoryExecution(displayCats.indexOf(catsArray[0]), -1, -1);
            }
            if (catsArray.length == 2) {
                categoryExecution(displayCats.indexOf(catsArray[0]), displayCats.indexOf(catsArray[1]), -1);
            }
            if (catsArray.length == 3) {
                categoryExecution(displayCats.indexOf(catsArray[0]), displayCats.indexOf(catsArray[1]),
                        displayCats.indexOf(catsArray[2]));
            }

        } else {
            if (catsArray.length == 0) {
                boxExecution(box, -1, -1);
            }
            if (catsArray.length == 1) {
                boxExecution(box, displayCats.indexOf(catsArray[0]), -1);
            }
            if (catsArray.length == 2) {
                boxExecution(box, displayCats.indexOf(catsArray[0]), displayCats.indexOf(catsArray[1]));
            }
        }
        if (resultList.getItems().isEmpty()) {
            resultList.getItems().add("No Emojis");
        }
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void openFile() {
        try {
            br = new BufferedReader(new FileReader("DisneyEmojiBlitzCategories1.3.csv"));
        } catch (FileNotFoundException e) {
            e.getStackTrace();
        }
    }

    private void categoryExecution(int c1, int c2, int c3) {
        try {
            String line = "";
            System.out.println("1" + br.equals(null));
            System.out.println("2" + br.readLine());
            br.readLine();
            while ((line = br.readLine()) != null) {
                String[] conditions = line.split(",");
                if (c2 == -1 && c3 == -1) {
                    if (conditions[c1 + 2].equals("Yes"))
                        results.add(conditions[0]);
                } else if (c3 == -1) {
                    if (conditions[c1 + 2].equals("Yes") && conditions[c2 + 2].equals("Yes"))
                        results.add(conditions[0]);
                } else {
                    if (conditions[c1 + 2].equals("Yes") && conditions[c2 + 2].equals("Yes")
                            && conditions[c3 + 2].equals("Yes"))
                        results.add(conditions[0]);
                }

            }
            for (String emoji : results) {
                resultList.getItems().add(emoji);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void boxExecution(String box, int c1, int c2) {
        try {
            String line = "";
            br.readLine();
            while ((line = br.readLine()) != null) {
                String[] conditions = line.split(",");
                if (c1 == -1 && c2 == -1) {
                    if (conditions[1].equals(box))
                        results.add(conditions[0]);
                } else if (c2 == -1) {
                    if (conditions[1].equals(box) && conditions[c1 + 2].equals("Yes"))
                        results.add(conditions[0]);
                } else {
                    if (conditions[1].equals(box) && conditions[c1 + 2].equals("Yes")
                            && conditions[c2 + 2].equals("Yes"))
                        results.add(conditions[0]);
                }

            }
            for (String emoji : results) {
                resultList.getItems().add(emoji);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

And just in case my DisneyEBFX.fxml. I have it located in my src/main/resources

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>

<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="329.0" prefWidth="600.0" style="-fx-background-color: #8470bb#8470bb; -fx-border-color: black;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="FX.Controller">
  <columnConstraints>
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
  </columnConstraints>
  <rowConstraints>
    <RowConstraints maxHeight="194.0" minHeight="0.0" prefHeight="45.0" vgrow="SOMETIMES" />
      <RowConstraints maxHeight="194.0" minHeight="10.0" prefHeight="45.0" vgrow="SOMETIMES" />
    <RowConstraints maxHeight="281.0" minHeight="10.0" prefHeight="64.0" vgrow="SOMETIMES" />
    <RowConstraints maxHeight="304.0" minHeight="10.0" prefHeight="66.0" vgrow="SOMETIMES" />
      <RowConstraints maxHeight="304.0" minHeight="10.0" prefHeight="142.0" vgrow="SOMETIMES" />
  </rowConstraints>
   <children>
      <ComboBox fx:id="cat1" prefWidth="150.0" promptText="Category 1" GridPane.rowIndex="2" />
      <ComboBox fx:id="cat2" prefWidth="150.0" promptText="Category 2" GridPane.columnIndex="1" GridPane.rowIndex="2" />
      <ComboBox fx:id="cat3" prefWidth="150.0" promptText="Category 3" GridPane.columnIndex="2" GridPane.rowIndex="2" />
      <ComboBox fx:id="box" prefWidth="150.0" promptText="Box" GridPane.columnIndex="3" GridPane.rowIndex="2" />
      <Button fx:id="compute" mnemonicParsing="false" onMousePressed="#computeButtonPressed" prefHeight="27.0" prefWidth="193.0" text="Compute" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.halignment="CENTER" GridPane.rowIndex="3" GridPane.valignment="TOP" GridPane.vgrow="NEVER" />
      <Label alignment="CENTER" prefHeight="17.0" prefWidth="204.0" text="Disney Emoji Blitz Categories" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
         <font>
            <Font name="Arial Bold" size="14.0" />
         </font>
      </Label>
      <ListView fx:id="resultList" prefHeight="129.0" prefWidth="326.0" GridPane.columnSpan="2147483647" GridPane.hgrow="NEVER" GridPane.rowIndex="4" GridPane.vgrow="NEVER" />
      <Label alignment="CENTER" prefHeight="17.0" prefWidth="186.0" text="Category 1" GridPane.rowIndex="1">
         <font>
            <Font name="Arial Bold" size="13.0" />
         </font>
      </Label>
      <Label alignment="CENTER" prefHeight="17.0" prefWidth="186.0" text="Category 2" GridPane.columnIndex="1" GridPane.rowIndex="1">
         <font>
            <Font name="Arial Bold" size="13.0" />
         </font>
      </Label>
      <Label alignment="CENTER" prefHeight="17.0" prefWidth="186.0" text="Category 3" GridPane.columnIndex="2" GridPane.rowIndex="1">
         <font>
            <Font name="Arial Bold" size="13.0" />
         </font>
      </Label>
      <Label alignment="CENTER" prefHeight="17.0" prefWidth="186.0" text="Box" GridPane.columnIndex="3" GridPane.rowIndex="1">
         <font>
            <Font name="Arial Bold" size="13.0" />
         </font>
      </Label>
   </children>
</GridPane>

If someone could help me out or at least give some suggestions, I would greatly appreciate it.

  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – itwasntme Feb 08 '20 at 11:10
  • `br.equals(null)` will trigger a NPE if `br` is null. Maybe you meant `(br == null)` – Mark Rotteveel Feb 08 '20 at 11:31

0 Answers0