java - HashMap 抛出 NullException 甚至 Map.containsKey 返回 true
问题描述
我在NullPointerException
使用正确的字符串键检索值时得到。
这是我的代码:
公式类
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());
}
}
}
控制器类。(我删除了一些初始化)
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));
});
}
}
您可以在此处看到此 屏幕截图上的密钥拼写正确。
堆栈跟踪
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)
解决方案
推荐阅读
- amazon-web-services - 无法在 AWS 数据管道中使用的 AWS EC2 机器上下载 pip 和 boto3
- python - IndexError:索引超出范围(创建新列)
- github-api - 这是为新用户复制仓库的正确方法吗?
- slack-api - 如何使用 SCIM API 更新用户的当前状态?
- php - 如何同时进行多个api调用?
- javascript - gitlab api更新文件nodejs:更改特定行
- c++ - 使用 CMake 在 MacOS 应用程序中链接 GLFW 的正确方法是什么?
- azure - 将 azure 函数应用身份验证链接到不同租户下的应用注册?
- pandas - pandas - 按列重复模式分组元素
- apache-atlas - 如何使用 apache atlas api 向现有实体添加分类?