-1

I have read and (believe I have) understood the posts made previously on this forum.

However as a Java novice I am having an issue

I create a MAP voucherDetails and pass it into a function where it is being populated (verified with trace). On return iy is empty. Why?

Map<String, Object> voucherDetails=new LinkedHashMap<>();
log.info("B4: "+voucherDetails.size());   // size is 0  
log.info("B4: "+voucherDetails.toString());     // {}
if (!ws.handleVoucherDetailsResponse(voucherDetails)){
    return false;
}
log.info("AF: "+voucherDetails.toString()); //{}
log.info("AF: "+voucherDetails.size());     // size is 0


public boolean handleVoucherDetailsResponse(Map<String, Object> voucher) throws IOException {
    log.enter("handleDetailsDetailsResponse");

    PCVMResponseType status=decodeResponse();
    switch (status) {       
    case DATA:
        voucher=decodeVoucherDetailsResponse(response);
        String accountno=(String)voucher.get("accountno");
        log.info("AA: "+accountno);
        String voucherno=(String)voucher.get("voucherno");
        log.info("VV: "+voucherno);         
        log.info("CC: "+voucher.size());
        log.info("IN: "+voucher.toString());            
        return true;
    default:
        return false;           
    }       
}

although voucher is being populated inside the function it is still empy on return. As if the Object is being passed not as a pointer to it.

What am I doing wrong and how do I do it correctly

TIA

Ephraim

Ephraim
  • 189
  • 1
  • 2
  • 17

2 Answers2

2

voucher=decodeVoucherDetailsResponse(response); does not update the caller's reference. You can't update the caller's reference without returning a new Map. However, you can add all the key-value pairs from the second Map to the first. Like,

voucher.putAll(decodeVoucherDetailsResponse(response));

If you want to remove all of the existing values as well, call voucher.clear(); before putAll().

Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226
  • So what does voucher=decodeVoucherDetailsResponse(response); actually do? It appears to have updated a map as the debug misleadinfly shows. What was updated? – Ephraim May 19 '17 at 16:25
  • The *local* reference in `handleVoucherDetailsResponse`, not the reference passed to `handleVoucherDetailsResponse`. Java is **always** pass by value, and you can't update the original caller's value (without returning and assigning, as you did with `decodeVoucherDetailsResponse`). – Elliott Frisch May 19 '17 at 16:26
0

Here is how java works:
All your integer, char, etc. variables are just plane values
All your objects variables are references

Here:

voucher=decodeVoucherDetailsResponse(response);

in function handleVoucherDetailsResponse() you set your voucher variable to the reference of the new object, returned by decodeVoucherDetailsResponse() .

So now you have two objects:
One that is referenced outside of the function (Empty)
And another that is referenced inside the function (Has data)

The way you should do that is to pass the voucher variable to the function, that will populate it.

If the Map is still empty, then there is a problem in decodeVoucherDetailsResponse() function.

Hope this helps you,
-Mk Km

Mk Km
  • 64
  • 5