Java-HashMap.get在不应该返回null时返回null

时间:2015-06-23 10:12:50

标签: java android null hashmap

我使用HashMap将一些字符串链接到整数,然后用于索引列表。 我在.get()中使用的字符串从android spinner传递给一个方法,当我将它们输出到日志时它们肯定是正确的,并且它们与HashMap中的那些匹配。 但是,使用其中一个字符串调用.get会返回null,并且当我尝试在计算中使用null整数时,会导致app force关闭。

 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$1.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中。

感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

这看起来像问题:

$smtpServer = "xyz.bla.com"
$smtpFrom = "No-reply@bla.com"
$smtpTo = "abc@mail.com"
$messageSubject = "Space notification"

$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true 

$data = Get-Content D:\output.txt | select -Skip 1 | ForEach-Object{
    $splitLine = $_ -split '\s+'
    [pscustomobject] @{
        'SERVER_NAME' = $splitLine[0]
        'MOUNT_POINT' = $splitLine[1]
        'TOTAL_SPACE' = $splitLine[2]
        'USED_SPACE' = $splitLine[3]
        'USED_PERCENTAGE' = $splitLine[4]
        'AVAILABLE_SPACE' = $splitLine[5]
    }
}

$headerStyle = 'style = "border: 1px solid black; background: #dddddd; padding: 5px;"'
$normalDataStyle =  'style = "border: 1px solid black; padding: 5px;text-align:center;"'
$backupRowStyle = 'bgcolor="#81F7F3"'
$normalRowStyle = 'bgcolor="#D0F5A9"'
$body = "<table style=`"border: 1px solid black; border-collapse: collapse;`">`r`n"
$body = $body + "<tr><th $headerStyle>SERVER_NAME</th><th $headerStyle>MOUNT_POINT</th><th $headerStyle>TOTAL_SPACE</th><th $headerStyle>USED_SPACE</th><th $headerStyle>USED_PERCENTAGE</th><th $headerStyle>AVAILABLE_SPACE</th></tr>`r`n"

$data | ForEach-Object{
    Switch($_.CLUSTER_NAME){
        "Server_1"{$rowStyle = $normalRowStyle}
        "Server_2"{$rowStyle = $normalRowStyle}
        "Server_3"{$rowStyle = $normalRowStyle}
        "Backup_server"{$rowStyle = $backupRowStyle}
        default{$rowStyle = ""}
    }
    $body = $body + "<tr $rowstyle><td $normalDataStyle>$($_.SERVER_NAME)</td><td $normalDataStyle>$($_.MOUNT_POINT)</td><td $normalDataStyle>$($_.TOTAL_SPACE)</td><td $normalDataStyle>$($_.USED_SPACE)</td><td $normalDataStyle>$($_.USED_PERCENTAGE)</td><td $normalDataStyle>$($_.AVAILABLE_SPACE)</td></tr>`r`n"
}

$body = '<body>' + $body + '</table><br/><br/><b>NOTE:</b> This is an automatically generated email, please do not reply to it.</body>'
$message.Body = $Body

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)

您正在初始化传递给该方法的本地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);
}
相关问题