0

I am getting NullPointerException when retrieving a value with the correct string key.

Here is my code:

Formula class

public class Formula {

    private Map<String, String> formulaMap = new HashMap<>();

    public Formula () {
        formulaMap.put("BinaryDecimal", "...");
        formulaMap.put("DecimalBinary", "...");
        printAll();
    }

    public String getFormula(String key) {
        System.out.println(formulaMap.containsKey(key));
        return formulaMap.get(key);
    }

    // just for printing all key-value to make sure it's not empty
    public void printAll () {
        for (Map.Entry<String, String> formula : formulaMap.entrySet()) {
            System.out.println(formula.getKey() + " : " + formula.getValue());
        }
    }
}

Contoller class. (I removed a few initializations)

public class Controller {
    Formula formulaMap = new Formula();

    @FXML
    private ComboBox<String> combo1;

    @FXML
    private ComboBox<String> combo2;

    @FXML
    public void initialize () {
        combo1.getSelectionModel().selectedIndexProperty().addListener((observableValue, s, t1) -> {
            System.out.println("\ncombo1.getSelectionModel().selectedIndexProperty");
            if (observableValue.getValue().intValue() == combo2.getSelectionModel().getSelectedIndex()) {
                combo2.getSelectionModel().select((Integer) s);
            }

            String formulaKey = combo1.getValue() + combo2.getValue();
            System.out.println(formulaKey);

            // method call that throws the NullPointerException
            formulaLabel.setText(formulaMap.getFormula(formulaKey)); 

        });
    }
}

You can see here that the key is correctly spelled on this screenshot.

Stacktrace

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at sample.Controller.lambda$initialize$0(Controller.java:55)
    at javafx.base/com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:360)
    at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
    at javafx.base/javafx.beans.property.ReadOnlyIntegerPropertyBase.fireValueChangedEvent(ReadOnlyIntegerPropertyBase.java:72)
    at javafx.base/javafx.beans.property.ReadOnlyIntegerWrapper.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:102)
    at javafx.base/javafx.beans.property.IntegerPropertyBase.markInvalid(IntegerPropertyBase.java:114)
    at javafx.base/javafx.beans.property.IntegerPropertyBase.set(IntegerPropertyBase.java:148)
    at javafx.controls/javafx.scene.control.SelectionModel.setSelectedIndex(SelectionModel.java:69)
    at javafx.controls/javafx.scene.control.SingleSelectionModel.updateSelectedIndex(SingleSelectionModel.java:215)
    at javafx.controls/javafx.scene.control.SingleSelectionModel.select(SingleSelectionModel.java:149)
    at sample.Controller.initialize(Controller.java:63)
    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:566)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2591)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
    at sample.Main.start(Main.java:14)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:834)
snich
  • 5
  • 3
  • 5
    Please read [mcve] and provide an example that we can run ourselves, not just a fragment of code. Looking at your code, there is no way how that line can throw that exception. The only reason for an exception in that line would be `formulaMap` to be null, which it cant, as the call before worked. Something is wrong here, and thus your input isn't sufficient to help you. – GhostCat Oct 04 '21 at 06:30
  • 1
    Can you provide the stacktrace as well ? – Kris Oct 04 '21 at 06:30
  • 1
    And just to be sure: you are using the standard java HashMap class, not some self written class that uses that name? – GhostCat Oct 04 '21 at 06:31
  • @GhostCat I updated my post. I'm using `java.util` for both `Map` and `HashMap`. – snich Oct 04 '21 at 06:51
  • 1
    Are you sure about formulaLabel is not null ? Did you debug by any chance ? – Kris Oct 04 '21 at 07:12
  • 1
    The error doesn't happen in HashMap, it happens on line 55 of `Controller.java`, because you're dereferencing null at that point. – Mark Rotteveel Oct 04 '21 at 08:43

0 Answers0