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)