Home > OS >  why does it give me the error that it is null
why does it give me the error that it is null

Time:06-27

I am trying to create comboboxes using initializable and i keep getting the error that it could be null and i dont know why, it used to work just fine but it something made it bug and i cant seem to find it for any reason.I have the error the code and the fxml pasted below any help would be appreciated

Error:

C:\Users\user\.jdks\openjdk-18.0.1.1\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.1\lib\idea_rt.jar=51551:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.1\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\user\.m2\repository\org\openjfx\javafx-controls\18\javafx-controls-18.jar;C:\Users\user\.m2\repository\org\openjfx\javafx-graphics\18\javafx-graphics-18.jar;C:\Users\user\.m2\repository\org\openjfx\javafx-base\18\javafx-base-18.jar;C:\Users\user\.m2\repository\org\openjfx\javafx-fxml\18\javafx-fxml-18.jar;C:\Users\user\.m2\repository\org\openjfx\javafx-web\18\javafx-web-18.jar;C:\Users\user\.m2\repository\org\openjfx\javafx-media\18\javafx-media-18.jar;C:\Users\user\.m2\repository\org\jetbrains\annotations\13.0\annotations-13.0.jar;C:\Users\user\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-common\1.5.32\kotlin-stdlib-common-1.5.32.jar -p C:\Users\user\.m2\repository\com\gluonhq\attach\audio\4.0.9\audio-4.0.9.jar;C:\Users\user\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib\1.5.32\kotlin-stdlib-1.5.32-modular.jar;C:\Users\user\.m2\repository\org\kordamp\ikonli\ikonli-core\12.3.0\ikonli-core-12.3.0.jar;C:\Users\user\.m2\repository\com\github\almasb\fxgl-entity\17\fxgl-entity-17.jar;C:\Users\user\.m2\repository\com\gluonhq\attach\util\4.0.9\util-4.0.9.jar;C:\Users\user\.m2\repository\com\github\almasb\fxgl\17\fxgl-17.jar;C:\Users\user\.m2\repository\org\openjfx\javafx-fxml\18\javafx-fxml-18-win.jar;C:\Users\user\.m2\repository\net\synedra\validatorfx\0.2.1\validatorfx-0.2.1.jar;C:\Users\user\.m2\repository\org\controlsfx\controlsfx\11.1.1\controlsfx-11.1.1.jar;C:\Users\user\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.12.1\jackson-core-2.12.1.jar;C:\Users\user\.m2\repository\org\kordamp\bootstrapfx\bootstrapfx-core\0.4.0\bootstrapfx-core-0.4.0.jar;C:\Users\user\.m2\repository\org\openjfx\javafx-graphics\18\javafx-graphics-18-win.jar;C:\Users\user\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.12.1\jackson-databind-2.12.1.jar;C:\Users\user\.m2\repository\org\openjfx\javafx-web\18\javafx-web-18-win.jar;C:\Users\user\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.12.1\jackson-annotations-2.12.1.jar;C:\Users\user\.m2\repository\org\kordamp\ikonli\ikonli-javafx\12.3.0\ikonli-javafx-12.3.0.jar;C:\Users\user\.m2\repository\com\github\almasb\fxgl-gameplay\17\fxgl-gameplay-17.jar;C:\Users\user\.m2\repository\org\openjfx\javafx-media\18\javafx-media-18-win.jar;C:\Users\user\.m2\repository\com\gluonhq\attach\storage\4.0.9\storage-4.0.9.jar;C:\Users\user\.m2\repository\com\gluonhq\attach\lifecycle\4.0.9\lifecycle-4.0.9.jar;C:\Users\user\.m2\repository\com\dlsc\formsfx\formsfx-core\11.3.2\formsfx-core-11.3.2.jar;C:\Users\user\.m2\repository\com\github\almasb\fxgl-io\17\fxgl-io-17.jar;C:\Users\user\.m2\repository\com\github\almasb\fxgl-scene\17\fxgl-scene-17.jar;C:\Users\user\.m2\repository\org\openjfx\javafx-base\18\javafx-base-18-win.jar;G:\Java\Projects\JavaCafeteria\target\classes;C:\Users\user\.m2\repository\org\openjfx\javafx-controls\18\javafx-controls-18-win.jar;C:\Users\user\.m2\repository\com\github\almasb\fxgl-core\17\fxgl-core-17.jar;C:\Users\user\.m2\repository\eu\hansolo\tilesfx\11.48\tilesfx-11.48.jar -m com.example.javacafeteria/com.example.javacafeteria.HelloApplication
javafx.fxml.LoadException: 
/G:/Java/Projects/JavaCafeteria/target/classes/com/example/javacafeteria/userMenu.fxml
at javafx.fxml@18/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
at javafx.fxml@18/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2685)
at javafx.fxml@18/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml@18/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3331)
at javafx.fxml@18/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3287)
at javafx.fxml@18/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3255)
at javafx.fxml@18/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3227)
at javafx.fxml@18/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3203)
at javafx.fxml@18/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3196)
at com.example.javacafeteria/com.example.javacafeteria.HelloApplication.start(HelloApplication.java:17)
at javafx.graphics@18/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at javafx.graphics@18/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics@18/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics@18/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics@18/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics@18/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics@18/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.ComboBox.setItems(javafx.collections.ObservableList)" because "this.coffeeType1" is null
    at com.example.javacafeteria/com.example.javacafeteria.userController.initialize(userController.java:111)
    at javafx.fxml@18/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2655)
    ... 17 more

My Code:

  package com.example.javacafeteria;

import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class userController implements Initializable {

private Stage stage;
private Scene scene;
private Parent root;

@FXML
private ComboBox<String> bevExtras;

@FXML
private Button bevOrder;

@FXML
private TextField bevPrice;

@FXML
private ComboBox<String> bevSize;

@FXML
private ComboBox<String> bevSweet;

@FXML
private ComboBox<String> bevSweetNo;

@FXML
private ComboBox<String> bevSyrup;

@FXML
private ComboBox<String> bevTemp;

@FXML
private ComboBox<String> bevType;

@FXML
private ComboBox<String> coffeeDosage1;

@FXML
private ComboBox<String> coffeeExtras1;

@FXML
private Button coffeeOrder1;

@FXML
private TextField coffeePrice1;

@FXML
private ComboBox<String> coffeeSweet1;

@FXML
private ComboBox<String> coffeeSweetNo1;

@FXML
private ComboBox<String> coffeeSyrup1;

@FXML
private ComboBox<String> coffeeTemp1;

@FXML
private ComboBox<String> coffeeType1;

@FXML
private Button newOrder;

@FXML
private Button switchEmployee;

@FXML
private ImageView trackOrder;

@FXML
private ImageView trackOrder1;

public void switchToScene1(ActionEvent event) throws IOException {
    root = FXMLLoader.load(getClass().getResource("mainMenu.fxml"));
    stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}

public void newOrderButton(ActionEvent event) throws IOException {
    root = FXMLLoader.load(getClass().getResource("userNewOrder.fxml"));
    stage = (Stage)((Node)event.getSource()).getScene().getWindow();
    scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
    coffeeType1.setItems(FXCollections.observableArrayList("Instant","French","Espresso","Cappuccino"));
    bevType.setItems(FXCollections.observableArrayList("Tea","Chocolate","Soda"));
    coffeeSweet1.setItems(FXCollections.observableArrayList("Sugar","Brown Sugar","Stevia"));
    bevSweet.setItems(FXCollections.observableArrayList("Sugar","Brown Sugar","Stevia"));
    coffeeSweetNo1.setItems(FXCollections.observableArrayList("1","2","3"));
    bevSweetNo.setItems(FXCollections.observableArrayList("1","2","3"));
    coffeeDosage1.setItems(FXCollections.observableArrayList("1","2"));
    bevSize.setItems(FXCollections.observableArrayList("Small - 12oz","Medium - 16oz","Large - 18oz"));
    coffeeTemp1.setItems(FXCollections.observableArrayList("Cold","Hot"));
    bevTemp.setItems(FXCollections.observableArrayList("Cold","Hot"));
    coffeeExtras1.setItems(FXCollections.observableArrayList("Whipped Cream","Cinnamon","Chocolate"));
    bevExtras.setItems(FXCollections.observableArrayList("Whipped Cream","Cinnamon","Chocolate"));
}
}

FXML File:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<fx:root maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="523.0" prefWidth="950.0" type="AnchorPane" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.javacafeteria.userController">
   <children>
      <ImageView fitHeight="523.0" fitWidth="750.0" layoutX="201.0" layoutY="1.0" pickOnBounds="true">
         <image>
            <Image url="@../../../Images/BackgroundCafe.png" />
         </image>
      </ImageView>
      <Button fx:id="newOrder" cacheShape="false" layoutX="-1.0" layoutY="217.0" mnemonicParsing="false" prefHeight="45.0" prefWidth="202.0" style="-fx-background-color: #0000;" text="New Order" textAlignment="RIGHT">
         <font>
            <Font size="15.0" />
         </font>
         <graphic>
            <ImageView fitHeight="24.0" fitWidth="24.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@../../../Images/icons8_plus_ _26px.png" />
               </image>
            </ImageView>
         </graphic></Button>
      <Button cache="true" layoutX="-2.0" layoutY="262.0" mnemonicParsing="false" prefHeight="45.0" prefWidth="202.0" style="-fx-background-color: #0000;" text="Track Order" textAlignment="RIGHT">
         <font>
            <Font size="15.0" />
         </font>
         <graphic>
            <ImageView fx:id="trackOrder" fitHeight="25.0" fitWidth="28.0" opacity="0.6" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@../../../Images/66841.png" />
               </image>
            </ImageView>
         </graphic></Button>
      <ImageView fitHeight="94.0" fitWidth="154.0" layoutX="22.0" layoutY="8.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@../../../Images/logocafe.png" />
         </image>
      </ImageView>
      <Button fx:id="switchEmployee" cache="true" layoutX="-1.0" layoutY="477.0" mnemonicParsing="false" onAction="#switchToScene1" prefHeight="45.0" prefWidth="202.0" style="-fx-background-color: #0000;" text="Switch to Employee" textAlignment="RIGHT">
         <font>
            <Font size="15.0" />
         </font>
         <graphic>
            <ImageView fx:id="trackOrder1" fitHeight="25.0" fitWidth="28.0" opacity="0.6" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@../../../Images/Sample_User_Icon.png" />
               </image>
            </ImageView>
         </graphic>
      </Button>
      <ComboBox fx:id="coffeeType1" layoutX="313.0" layoutY="285.0" prefHeight="25.0" prefWidth="189.0" promptText="Type" style="-fx-background-color: #00000;" />
      <Text layoutX="355.0" layoutY="87.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Coffee" wrappingWidth="109.61000061035156">
         <font>
            <Font name="AppleGothic Regular" size="35.0" />
         </font>
      </Text>
      <ComboBox fx:id="coffeeSweet1" layoutX="312.0" layoutY="348.0" prefHeight="25.0" prefWidth="120.0" promptText="Sweetener" />
      <ImageView fitHeight="25.0" fitWidth="25.0" layoutX="287.0" layoutY="411.0">
         <image>
            <Image url="@../../../Images/icons8_spoon_208px.png" />
         </image>
      </ImageView>
      <ImageView fitHeight="25.0" fitWidth="25.0" layoutX="287.0" layoutY="478.0">
         <image>
            <Image url="@../../../Images/icons8_euro_480px_1.png" />
         </image>
      </ImageView>
      <ComboBox fx:id="coffeeSweetNo1" layoutX="432.0" layoutY="348.0" prefHeight="25.0" prefWidth="70.0" promptText="No." />
      <ComboBox fx:id="coffeeTemp1" layoutX="313.0" layoutY="381.0" prefHeight="25.0" prefWidth="189.0" promptText="Temperature" />
      <ImageView fitHeight="25.0" fitWidth="25.0" layoutX="286.0" layoutY="448.0">
         <image>
            <Image url="@../../../Images/icons8_more_208px.png" />
         </image>
      </ImageView>
      <ComboBox fx:id="coffeeDosage1" layoutX="313.0" layoutY="413.0" prefHeight="25.0" prefWidth="189.0" promptText="Dosage" />
      <ComboBox fx:id="bevType" layoutX="620.0" layoutY="285.0" prefHeight="25.0" prefWidth="189.0" promptText="Type" style="-fx-background-color: #00000;" />
      <ImageView fitHeight="25.0" fitWidth="25.0" layoutX="287.0" layoutY="382.0">
         <image>
            <Image url="@../../../Images/icons8_temperature_512px.png" />
         </image>
      </ImageView>
      <ImageView fitHeight="165.0" fitWidth="165.0" layoutX="632.0" layoutY="118.0">
         <image>
            <Image url="@../../../Images/icons8_tea_144px.png" />
         </image>
      </ImageView>
      <ImageView fitHeight="25.0" fitWidth="25.0" layoutX="286.0" layoutY="315.0">
         <image>
            <Image url="@../../../Images/icons8_soap_dispenser_480px.png" />
         </image>
      </ImageView>
      <ImageView fitHeight="165.0" fitWidth="165.0" layoutX="331.0" layoutY="102.0">
         <image>
            <Image url="@../../../Images/icons8_coffee_500px.png" />
         </image>
      </ImageView>
      <ComboBox fx:id="bevSize" layoutX="620.0" layoutY="411.0" prefHeight="25.0" prefWidth="189.0" promptText="Size" />
      <Text layoutX="645.0" layoutY="88.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Beverage" wrappingWidth="151.7384033203125">
         <font>
            <Font name="AppleGothic Regular" size="35.0" />
         </font>
      </Text>
      <ImageView fitHeight="25.0" fitWidth="25.0" layoutX="594.0" layoutY="285.0">
         <image>
            <Image url="@../../../Images/icons8_water_glass_480px.png" />
         </image>
      </ImageView>
      <ComboBox fx:id="coffeeExtras1" layoutX="313.0" layoutY="446.0" prefHeight="25.0" prefWidth="189.0" promptText="Extras" />
      <ImageView fitHeight="25.0" fitWidth="25.0" layoutX="596.0" layoutY="411.0">
         <image>
            <Image url="@../../../Images/icons8_metamorphose_480px.png" />
         </image>
      </ImageView>
      <Button fx:id="coffeeOrder1" layoutX="410.0" layoutY="478.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="90.0" text="Order" />
      <TextField fx:id="coffeePrice1" editable="false" layoutX="313.0" layoutY="478.0" prefHeight="25.0" prefWidth="100.0" promptText="Price" />
      <TextField fx:id="bevPrice" editable="false" layoutX="621.0" layoutY="478.0" prefHeight="25.0" prefWidth="100.0" promptText="Price" />
      <Button fx:id="bevOrder" layoutX="721.0" layoutY="478.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="90.0" text="Order" />
      <ImageView fitHeight="20.0" fitWidth="25.0" layoutX="287.0" layoutY="288.0">
         <image>
            <Image url="@../../../Images/icons8_coffee_beans_144px.png" />
         </image>
      </ImageView>
      <ImageView fitHeight="25.0" fitWidth="25.0" layoutX="286.0" layoutY="347.0">
         <image>
            <Image url="@../../../Images/icons8_sugar_cubes_500px.png" />
         </image>
      </ImageView>
      <ImageView fitHeight="25.0" fitWidth="25.0" layoutX="596.0" layoutY="478.0">
         <image>
            <Image url="@../../../Images/icons8_euro_480px_1.png" />
         </image>
      </ImageView>
      <ComboBox fx:id="coffeeSyrup1" layoutX="313.0" layoutY="316.0" prefHeight="25.0" prefWidth="189.0" promptText="Syrup" />
      <ImageView fitHeight="25.0" fitWidth="25.0" layoutX="594.0" layoutY="316.0">
         <image>
            <Image url="@../../../Images/icons8_soap_dispenser_480px.png" />
         </image>
      </ImageView>
      <ComboBox fx:id="bevSyrup" layoutX="620.0" layoutY="315.0" prefHeight="25.0" prefWidth="189.0" promptText="Syrup" />
      <ImageView fitHeight="25.0" fitWidth="25.0" layoutX="595.0" layoutY="347.0">
         <image>
            <Image url="@../../../Images/icons8_sugar_cubes_500px.png" />
         </image>
      </ImageView>
      <ComboBox fx:id="bevSweet" layoutX="621.0" layoutY="347.0" prefHeight="25.0" prefWidth="120.0" promptText="Sweetener" />
      <ComboBox fx:id="bevSweetNo" layoutX="741.0" layoutY="348.0" prefHeight="25.0" prefWidth="70.0" promptText="No." />
      <ComboBox fx:id="bevTemp" layoutX="620.0" layoutY="381.0" prefHeight="25.0" prefWidth="189.0" promptText="Temperature" />
      <ImageView fitHeight="25.0" fitWidth="25.0" layoutX="596.0" layoutY="381.0">
         <image>
            <Image url="@../../../Images/icons8_temperature_512px.png" />
         </image>
      </ImageView>
      <ComboBox fx:id="bevExtras" layoutX="620.0" layoutY="446.0" prefHeight="25.0" prefWidth="189.0" promptText="Extras" />
      <ImageView fitHeight="25.0" fitWidth="25.0" layoutX="596.0" layoutY="446.0">
         <image>
            <Image url="@../../../Images/icons8_more_208px.png" />
         </image>
      </ImageView>
   </children>
</fx:root>

CodePudding user response:

Edited: Not accepted.

I suppose you are using a member without initialization so you might need to initialize the coffeeType1 like this

@FXML
private ComboBox<String> coffeeType1 = new ComboBox<>();

See an example: https://github.com/AliIbrahim996/dom_parser/blob/main/src/sample/Controller.java#L25

  • Related