3

I am facing a very strange situation. I add an object to an arrayList in a loop, but it is replaced by the next object. Actually second item is duplicated. ( It replaces the first item as well as inserts another object to the ArrayList.)

This is my code. I have done the debugging and included the comments where needed. Could someone point out why this happens? I am taking the object details from the database and those are working as expected.

public class Serv
{
    @Autowired
    GrpHeader objGrpHeader;

    @Autowired
    CompPesoOutgoingMsg objMsg;

    @Autowired
    OutwardMessage objOutwardMessage;

    public List<OutwardMessage> outgoingMessagesAsSingleTrx()
    {
        List<OutgoingMsg_Obj> trxList = myRepo.getTrx("5");
        List<OutwardMessage> myTrxList = new ArrayList<>();


        for (OutgoingMsg_Obj outgoingMsg : trxList)
        {
            BigDecimal trxAmt = outgoingMsg.getIntrBkSttlmAmt().getTrxn_amt();
            trxAmt = (trxAmt).divide(new BigDecimal(100));

            GrpHeader grpHeader = objGrpHeader;
            CompPesoOutgoingMsg outMsg2 = objMsg;
            OutwardMessage objOutwardMessage2 = objOutwardMessage;

            outgoingMsg.setRmtInf(objRmtInf);
            outgoingMsg.setPmtTpInf(objPmtTpInf);

            outMsg2.setHeader(grpHeader);
            outMsg2.setCdtTrfTxInf(Arrays.asList(outgoingMsg));
            objOutwardMessage2.setObjMsg(outMsg2);

            **//Here, Correct object details are printed**
            log.info("outwardMsg 100 {} ", objOutwardMessage2);

            //Add Item to the list
            myTrxList.add(objOutwardMessage2);

            for (OutwardMessage outwardMsgx : myTrxList)
            {
                //1. When this loop executed first time, first object details are printed
                //2. When printed second time, first added object is no more. And second added object is there twice.
                log.info("outwardMsg 101 {} ", outwardMsgx);
            }
        }
        return myTrxList;
    }
}
Ran_Macavity
  • 154
  • 2
  • 21
  • 1
    You use a single `objOutwardMessage`. You only mutate that single object. (PS.: please drop the Hungarian notion, it makes things very hard to read) – Johannes Kuhn Jun 02 '20 at 09:53
  • Thanks for the answer.I am sorry about the format.Any idea how to fix it? I am kinda new to springboot – Ran_Macavity Jun 02 '20 at 09:57
  • 1
    Does this answer your question? [Why does my ArrayList contain N copies of the last item added to the list?](https://stackoverflow.com/questions/19843506/why-does-my-arraylist-contain-n-copies-of-the-last-item-added-to-the-list) – Ivar Jun 02 '20 at 09:58

2 Answers2

1

You have a single reference. By setting the objOutwardMessage2to objOutwardMessageyou are just changing the data inside the reference.

fjsv
  • 705
  • 10
  • 23
  • Thanks for the answer. Any idea how to fix it? I am kinda new to springboot – Ran_Macavity Jun 02 '20 at 09:57
  • 1
    For what I can gather, you should create a new instance of `OutwardMessage`instead of: `OutwardMessage objOutwardMessage2 = objOutwardMessage;` doing this. Depending on what you are trying to achieve you may need something else since the `OutwardMessage` is a bean. If you need the data inside that object, take a look at: [this question](https://stackoverflow.com/questions/2624165/how-to-copy-an-object-by-value-not-by-reference) – fjsv Jun 02 '20 at 10:03
1

Since no new object is created for each iteration, the same objOutwardMessage2 value is getting replaced each time.

Try

OutwardMessage objOutwardMessage2 = new OutwardMessage();

and copy the value of objOutwardMessage to the newly created objOutwardMessage2.

Sreejith
  • 506
  • 5
  • 7