Java- HashMap.get returns null 不应该的时候
Java- HashMap.get returns null when it shouldn't
我正在使用 HashMap 将一些字符串 link 转换为整数,然后将其用于索引列表。
我在 .get() 中使用的字符串从 android 微调器传递给方法,并且在我将它们输出到日志时绝对正确,并且它们与 HashMap 中的字符串匹配。
但是,使用这些字符串之一调用 .get returns null,随后当我尝试在计算中使用 null 整数时导致应用程序强制关闭。
HashMap<String, Integer> valuesOfAngleUnits = new HashMap<String, Integer>();
public void setValuesOfAngleUnits(HashMap<String, Integer> valuesOfAngleUnits) {
valuesOfAngleUnits.put("Arc minute", 0);
valuesOfAngleUnits.put("Arc second", 1);
valuesOfAngleUnits.put("Degree", 2);
valuesOfAngleUnits.put("Gon", 3);
valuesOfAngleUnits.put("Grad", 4);
valuesOfAngleUnits.put("Mil (Nato)", 5);
valuesOfAngleUnits.put("Mil (Soviet Union)", 6);
valuesOfAngleUnits.put("Octant", 7);
valuesOfAngleUnits.put("Quadrant", 8);
valuesOfAngleUnits.put("Radian", 9);
valuesOfAngleUnits.put("Revolution", 10);
valuesOfAngleUnits.put("Sextant", 11);
valuesOfAngleUnits.put("Sign", 12);
valuesOfAngleUnits.put("Turn", 13);
}
List<Double> convertToArcSeconds = new ArrayList(Arrays.asList(1, 0.1666667, 0.000046, 0.0166667, 0.18519, 0.18519, 0.296296, 0.277778, 0.291667, 0.00037, 0.000185, 0.000291, 0.000046, 0.000278, 0.000556, 0.000046));
public void calculateConversion(String inputSpinner, String outputSpinner, Float userInput){
Float output = new Float(0.0);
String Tag = "Input collected";
Log.v(Tag, "The input data type is " + inputSpinner + ". The output data type is " + outputSpinner + ", and the user input is " + userInput);
Integer numericalValueOfInputSpinner = valuesOfAngleUnits.get(inputSpinner);
Log.v(Tag, "The numerical value of the spinner is " + numericalValueOfInputSpinner);
Float ArcSecondValue = new Float(userInput * convertToArcSeconds.get(numericalValueOfInputSpinner));
String Tag2 = "ArcSecondValueFound";
Log.v(Tag2, "The arc second value is " + ArcSecondValue);
}
上面的代码设置了一个 hashmap 和一个列表,并向您展示了所使用的方法。
06-22 21:23:34.113 3728-3728/? V/Input collected﹕ The input data type is Arc minute. The output data type is Arc minute, and the user input is 7645.0
06-22 21:23:34.113 3728-3728/? V/Input collected﹕ The numerical value of the spinner is null
06-22 21:23:34.113 3728-3728/? D/AndroidRuntime﹕ Shutting down VM
06-22 21:23:34.114 3728-3728/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.anapp.tpb.sidebarproject, PID: 3728
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
at com.anapp.tpb.sidebarproject.MainActivity.calculateConversion(MainActivity.java:78)
at com.anapp.tpb.sidebarproject.MainActivity.onClick(MainActivity.java:100)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-22 21:23:34.114 1224-1388/? W/ActivityManager﹕ Force finishing activity 1 com.anapp.tpb.sidebarproject/.MainActivity
以上是 logcat 输出。前两行显示已将正确的字符串连同一个浮点数传递给该方法。
但是,数值为空。
我不明白为什么会这样,因为字符串 "Arc minute" 在 valuesOfAngleUnits HashMap 中。
感谢任何帮助。
这看起来像问题所在:
HashMap<String, Integer> valuesOfAngleUnits = new HashMap<String, Integer>();
public void setValuesOfAngleUnits(HashMap<String, Integer> valuesOfAngleUnits) {
valuesOfAngleUnits.put("Arc minute", 0);
valuesOfAngleUnits.put("Arc second", 1);
valuesOfAngleUnits.put("Degree", 2);
valuesOfAngleUnits.put("Gon", 3);
valuesOfAngleUnits.put("Grad", 4);
valuesOfAngleUnits.put("Mil (Nato)", 5);
valuesOfAngleUnits.put("Mil (Soviet Union)", 6);
valuesOfAngleUnits.put("Octant", 7);
valuesOfAngleUnits.put("Quadrant", 8);
valuesOfAngleUnits.put("Radian", 9);
valuesOfAngleUnits.put("Revolution", 10);
valuesOfAngleUnits.put("Sextant", 11);
valuesOfAngleUnits.put("Sign", 12);
valuesOfAngleUnits.put("Turn", 13);
}
您正在初始化传递给该方法的本地 HashMap,它可能与同名的实例变量不同。
改为:
public void setValuesOfAngleUnits() {
valuesOfAngleUnits.put("Arc minute", 0);
valuesOfAngleUnits.put("Arc second", 1);
valuesOfAngleUnits.put("Degree", 2);
valuesOfAngleUnits.put("Gon", 3);
valuesOfAngleUnits.put("Grad", 4);
valuesOfAngleUnits.put("Mil (Nato)", 5);
valuesOfAngleUnits.put("Mil (Soviet Union)", 6);
valuesOfAngleUnits.put("Octant", 7);
valuesOfAngleUnits.put("Quadrant", 8);
valuesOfAngleUnits.put("Radian", 9);
valuesOfAngleUnits.put("Revolution", 10);
valuesOfAngleUnits.put("Sextant", 11);
valuesOfAngleUnits.put("Sign", 12);
valuesOfAngleUnits.put("Turn", 13);
}
我正在使用 HashMap 将一些字符串 link 转换为整数,然后将其用于索引列表。 我在 .get() 中使用的字符串从 android 微调器传递给方法,并且在我将它们输出到日志时绝对正确,并且它们与 HashMap 中的字符串匹配。 但是,使用这些字符串之一调用 .get returns null,随后当我尝试在计算中使用 null 整数时导致应用程序强制关闭。
HashMap<String, Integer> valuesOfAngleUnits = new HashMap<String, Integer>();
public void setValuesOfAngleUnits(HashMap<String, Integer> valuesOfAngleUnits) {
valuesOfAngleUnits.put("Arc minute", 0);
valuesOfAngleUnits.put("Arc second", 1);
valuesOfAngleUnits.put("Degree", 2);
valuesOfAngleUnits.put("Gon", 3);
valuesOfAngleUnits.put("Grad", 4);
valuesOfAngleUnits.put("Mil (Nato)", 5);
valuesOfAngleUnits.put("Mil (Soviet Union)", 6);
valuesOfAngleUnits.put("Octant", 7);
valuesOfAngleUnits.put("Quadrant", 8);
valuesOfAngleUnits.put("Radian", 9);
valuesOfAngleUnits.put("Revolution", 10);
valuesOfAngleUnits.put("Sextant", 11);
valuesOfAngleUnits.put("Sign", 12);
valuesOfAngleUnits.put("Turn", 13);
}
List<Double> convertToArcSeconds = new ArrayList(Arrays.asList(1, 0.1666667, 0.000046, 0.0166667, 0.18519, 0.18519, 0.296296, 0.277778, 0.291667, 0.00037, 0.000185, 0.000291, 0.000046, 0.000278, 0.000556, 0.000046));
public void calculateConversion(String inputSpinner, String outputSpinner, Float userInput){
Float output = new Float(0.0);
String Tag = "Input collected";
Log.v(Tag, "The input data type is " + inputSpinner + ". The output data type is " + outputSpinner + ", and the user input is " + userInput);
Integer numericalValueOfInputSpinner = valuesOfAngleUnits.get(inputSpinner);
Log.v(Tag, "The numerical value of the spinner is " + numericalValueOfInputSpinner);
Float ArcSecondValue = new Float(userInput * convertToArcSeconds.get(numericalValueOfInputSpinner));
String Tag2 = "ArcSecondValueFound";
Log.v(Tag2, "The arc second value is " + ArcSecondValue);
}
上面的代码设置了一个 hashmap 和一个列表,并向您展示了所使用的方法。
06-22 21:23:34.113 3728-3728/? V/Input collected﹕ The input data type is Arc minute. The output data type is Arc minute, and the user input is 7645.0
06-22 21:23:34.113 3728-3728/? V/Input collected﹕ The numerical value of the spinner is null
06-22 21:23:34.113 3728-3728/? D/AndroidRuntime﹕ Shutting down VM
06-22 21:23:34.114 3728-3728/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.anapp.tpb.sidebarproject, PID: 3728
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
at com.anapp.tpb.sidebarproject.MainActivity.calculateConversion(MainActivity.java:78)
at com.anapp.tpb.sidebarproject.MainActivity.onClick(MainActivity.java:100)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-22 21:23:34.114 1224-1388/? W/ActivityManager﹕ Force finishing activity 1 com.anapp.tpb.sidebarproject/.MainActivity
以上是 logcat 输出。前两行显示已将正确的字符串连同一个浮点数传递给该方法。 但是,数值为空。 我不明白为什么会这样,因为字符串 "Arc minute" 在 valuesOfAngleUnits HashMap 中。
感谢任何帮助。
这看起来像问题所在:
HashMap<String, Integer> valuesOfAngleUnits = new HashMap<String, Integer>();
public void setValuesOfAngleUnits(HashMap<String, Integer> valuesOfAngleUnits) {
valuesOfAngleUnits.put("Arc minute", 0);
valuesOfAngleUnits.put("Arc second", 1);
valuesOfAngleUnits.put("Degree", 2);
valuesOfAngleUnits.put("Gon", 3);
valuesOfAngleUnits.put("Grad", 4);
valuesOfAngleUnits.put("Mil (Nato)", 5);
valuesOfAngleUnits.put("Mil (Soviet Union)", 6);
valuesOfAngleUnits.put("Octant", 7);
valuesOfAngleUnits.put("Quadrant", 8);
valuesOfAngleUnits.put("Radian", 9);
valuesOfAngleUnits.put("Revolution", 10);
valuesOfAngleUnits.put("Sextant", 11);
valuesOfAngleUnits.put("Sign", 12);
valuesOfAngleUnits.put("Turn", 13);
}
您正在初始化传递给该方法的本地 HashMap,它可能与同名的实例变量不同。
改为:
public void setValuesOfAngleUnits() {
valuesOfAngleUnits.put("Arc minute", 0);
valuesOfAngleUnits.put("Arc second", 1);
valuesOfAngleUnits.put("Degree", 2);
valuesOfAngleUnits.put("Gon", 3);
valuesOfAngleUnits.put("Grad", 4);
valuesOfAngleUnits.put("Mil (Nato)", 5);
valuesOfAngleUnits.put("Mil (Soviet Union)", 6);
valuesOfAngleUnits.put("Octant", 7);
valuesOfAngleUnits.put("Quadrant", 8);
valuesOfAngleUnits.put("Radian", 9);
valuesOfAngleUnits.put("Revolution", 10);
valuesOfAngleUnits.put("Sextant", 11);
valuesOfAngleUnits.put("Sign", 12);
valuesOfAngleUnits.put("Turn", 13);
}