0

I have written a code to fetch the value from a table and place it in a map with a key-value pair and return the map.

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;
    } 

This will return the Map with values like this

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

But when I trying to catch the same map and trying to get the value from it.

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

It will show null to every key-value pair like this

null

If anyone comes across the same issue please suggest me a solution.🙁

5
  • 1
    It's not possible. I'm sure that you are doing something else. Commented Mar 5, 2020 at 9:51
  • Since I have written this code for tool-specific. As java perspective, Could you please elaborate on what the issue here? Commented Mar 5, 2020 at 9:57
  • Could you use the debugger to check which elements the map mCartValue1 contains? (Or simply print it out) Commented Mar 5, 2020 at 10:17
  • It looks like you're getting the string values for the map from some external resource (a DOM maybe, or something like that). It's quite possible that the string that shows as "Subtotal" contains some non-visible characters in that version. Print out the length of each key and check it visually against the text to verify that no non-visible characters are included in there. Commented Mar 5, 2020 at 10:26
  • Can you do System.out.println(mCartValue1); on the line right before BigDecimal Subtotal1_1 = mCartValue1.get("Subtotal"); and add it to your post Commented Mar 5, 2020 at 10:32

1 Answer 1

2

I have tried with sample demo. It was working fine for me.

  1. Please use HashMap instead of HashedMap or verify what is HashedMap implementation.
  2. check whether String sCharges = RowValue.replaceAll("<REPLACE>", Integer.toString(i)); line returning the amount value properly like $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;
    } 
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response, I found the mistake it was on this line String sVariableName = client.getText(sVariablexpath).replace(":", ""); it will fetch the key name with spaces and after trimming it its working fine.
While Debug it it will show properly, But while printing the Map we will get to know about the extra space.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.