【问题标题】:Java hashmap giving null value even the key and value is not null即使键和值不为空,Java hashmap 也会给出空值
【发布时间】:2020-03-05 18:27:40
【问题描述】:

我编写了一段代码,用于从表中获取值并将其放置在带有键值对的映射中并返回映射。

public Map<String, BigDecimal> fnFetchCartSummary() throws Exception {
        Map<String, BigDecimal> mCartSummaryMap = new HashedMap<String, BigDecimal>();
        int iCartCount = client.getElementCount(CartTotal);
        if (iCartCount > 1) {
            client.ValidateTest(true, "Service charges are getting displayed for Cart. Total entries = " + iCartCount);
            for (int i = 1; i <= iCartCount; i++) {
                String sVariablexpath = sRowKey.replaceAll("<REPLACE>", Integer.toString(i));
                String sVariableName = client.getText(sVariablexpath).replace(":", "");
                String sCharges = RowValue.replaceAll("<REPLACE>", Integer.toString(i));
                double sPrice =Double.parseDouble( client.getText(sCharges).replaceAll("[^0-9.]+", ""));
                BigDecimal bdPrice = new BigDecimal(sPrice);
                mCartSummaryMap.put(sVariableName, bdPrice);
            }
        }
        return mCartSummaryMap;
    } 

这将返回具有类似值的地图

 {MPVPERTKTF=10, Subtotal=1200, MPVEVTF=15, Total Amount Due=1227, MPV SC001 SPD=2}

但是当我试图捕捉同一张地图并试图从中获取价值时。

 Map<String, BigDecimal> mCartValue1 = client.shoppingCart.fnFetchCartSummary();
BigDecimal Subtotal1_1 = mCartValue1.get("Subtotal");
System.out.println(Subtotal1_1);

它会像这样对每个键值对显示 null

如果有人遇到同样的问题,请给我建议一个解决方案。????

【问题讨论】:

  • 不可能。我确定你在做其他事情。
  • 因为我已经为特定工具编写了这段代码。作为java的角度,你能详细说明这里的问题吗?
  • 您可以使用调试器检查地图mCartValue1 包含哪些元素吗? (或者干脆打印出来)
  • 看起来您正在从某个外部资源(可能是 DOM 或类似的东西)获取地图的字符串值。显示为“小计”的字符串很可能包含该版本中的一些不可见字符。打印出每个键的长度并对照文本进行目视检查,以验证其中不包含不可见字符。
  • 你能不能在BigDecimal Subtotal1_1 = mCartValue1.get("Subtotal"); 前面的那一行做System.out.println(mCartValue1); 并将它添加到你的帖子中

标签: java null hashmap


【解决方案1】:

我已经尝试过示例演示。对我来说效果很好。

  1. 请使用HashMap 而不是HashedMap 或验证HashedMap 的实现方式。
  2. 检查String sCharges = RowValue.replaceAll("&lt;REPLACE&gt;", Integer.toString(i)); 行是否正确返回金额值,如$1200
public class TestHashMap {

    public static void main(String[] args) {
      Map<String, BigDecimal> mCartValue1 = fnFetchCartSummary();
      BigDecimal Subtotal1_1 = mCartValue1.get("Subtotal");
      System.out.println(Subtotal1_1);
    }


    public static Map<String, BigDecimal> fnFetchCartSummary() {
        Map<String, BigDecimal> mCartSummaryMap = new HashMap<String, BigDecimal>();

        //String sCharges = "Rs1200";
        double sPrice =Double.parseDouble( "Rs1200".replaceAll("[^0-9.]+", ""));
        BigDecimal bdPrice = new BigDecimal(sPrice);
        mCartSummaryMap.put("MPVPERTKTF", new BigDecimal(10));
        mCartSummaryMap.put("Subtotal", bdPrice);
        mCartSummaryMap.put("MPVEVTF", new BigDecimal(15));
        mCartSummaryMap.put("Total Amount Due", new BigDecimal(1227));

        return mCartSummaryMap;
    } 
}

【讨论】:

  • 感谢您的回复,我发现错误在这一行 String sVariableName = client.getText(sVariablexpath).replace(":", "");它将获取带有空格的键名,并在修剪后正常工作。
  • 在调试时会正常显示,但在打印地图时我们会了解额外的空间。
猜你喜欢
  • 1970-01-01
  • 2019-01-27
  • 2014-11-13
  • 1970-01-01
  • 2021-06-08
  • 2021-12-30
  • 2014-10-04
  • 1970-01-01
  • 2018-07-05
相关资源
最近更新 更多