0

There might be something very simple that I'm missing here but basically I have objects that I wish to store in an arrayList that is global. However when I leave the function, and try to access the object elements in this same arrayList, a field is being overwritten and I'm not sure how.

I've nailed down the problem to a specific region and will show the relevant code. The date (timeSlot\ Calendar) object is the indicator of this problem.

requestHandler(){  
        Pattern pattern = Pattern.compile("*irrelevant regex*");
        Matcher matcher = pattern.matcher(element);

        while(matcher.find()) {

            int requestID = Integer.parseInt(matcher.group(1));

            Calendar startDate = dateHandler(matcher.group(2));
            Calendar endDate = dateHandler(matcher.group(3));

            TimeSlot timeSlot = generateTimeSlot(startDate, endDate);           
            TransmissionRequest t_Request = transmissionHandler(matcher.group(4));

            BookingRequest request = new BookingRequest(requestID, timeSlot, t_Request);

            requestList.add(request);

            // debug //
            System.out.println(request);
        }
}

For debugging purposes I added the print statement at the very bottom, which gives me the correct output. Basically prints out timeSlot as a string for each BookingRequest. So clearly the request object with the correct timeSlot values are being added to requestList.

OUTPUT (I have left out the irrelevant fields)

BookingRequest:1
Date= 11:00 Mar-25 to 12:00 Mar-26

BookingRequest:2
Date= 12:00 Mar-24 to 03:00 Mar-27

BookingRequest:3
Date= 01:00 Mar-26 to 09:00 Mar-26

BookingRequest:4
Date= 11:00 Mar-25 to 09:00 Mar-26

BookingRequest:5
Date= 11:00 Mar-26 to 09:00 Mar-27

However, as soon as I leave this function and attempt to print this exact same output via the ArrayList requestList, I get the following.

OUTPUT

BookingRequest:1
Date= 11:00 Mar-26 to 09:00 Mar-27

BookingRequest:2
Date= 11:00 Mar-26 to 09:00 Mar-27

BookingRequest:3
Date= 11:00 Mar-26 to 09:00 Mar-27

BookingRequest:4
Date= 11:00 Mar-26 to 09:00 Mar-27

BookingRequest:5
Date= 11:00 Mar-26 to 09:00 Mar-27

It appears that each timeSlot object is being overwritten by the very last timeSlot object that is handled. I have not made any modifications to the array, and have ensured to obtain this output right after requestHandler() was called. Code below.

for(String element : input){

    // Extract requests
    if(element.matches("^Request.*")){
        requestHandler(element);
    }

}
for(BookingRequest request : requestList){
    System.out.println(request);
}

Here are also the relevant handler and generate functions if you believe the problem lies in them. Both create new instances of TimeSlot and Calendar, so I don't think there is an issue with reassignment of the same instance.

public static Calendar dateHandler(String stringExtract){
    Calendar date = new GregorianCalendar();
    SimpleDateFormat format = new SimpleDateFormat("HH MMM dd", Locale.ENGLISH);
    try {
        date.setTime(format.parse(stringExtract));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;    
}


public static TimeSlot generateTimeSlot(Calendar start, Calendar end){
    TimeSlot newTimeSlot = new TimeSlot(start, end);
    return newTimeSlot;
}

I have spent far too long trying to figure out what the exact problem is. I have a feeling it may also have something to do with the behaviour of matcher.find(), but I have quite literally exhausted myself trying everything to just identify what the exact problem is let alone fix it.

Any help would be more than appreciated!

J Z
  • 89
  • 1
  • 8

1 Answers1

2

Maybe you should check for static fields: Namely BookingRequest.timeSlot, or in TimeSlot itself.

Bernd Ebertz
  • 1,317
  • 8
  • 10
  • omg. I don't believe it. I had a static variable in my `BookingRequest` constructor, and I'm pretty sure I didn't type it in myself, but rather eclipse had a quick fix option that made it so and I never noticed it. Thanks so much! – J Z Apr 04 '17 at 00:01