I have a very strange situation. I´m working on a pretty big Java application with many bugs, and I found this one today.
I´ll try to explain the situation without posting code because the methods are way too long and I have identified and isolated the specific problem. Here it is:
I have a session attribute set on a Controller class. The attribute has several fields, a couple of Strings, a couple of int and an ArrayList of a certain object type. It is set like this:
request.getSession().setAttribute(Constants.SESSION_LIST_SEARCH, beanList);
Then there is another Controller class where I need to read this attribute, it goes like this:
request.getSession().getAttribute(Constants.SESSION_LIST_SEARCH);
When the controller gets the attribute (with the proper cast) the Strings and the int fields are there, but the ArrayList it´s empty.
I couldn’t find an answer so in an act of desperation I tried to “clone” the list to see what happened, so it goes like this:
request.getSession().setAttribute(Constants.SESSION_LIST_SEARCH, beanList);
/* Desperate developer */
ArrayList<ActivityBean> duplicatedList = new ArrayList<ActivityBean>();
for(ActivityBean foo:beanList.getActivityBean()){
duplicatedList.add(foo);
}
request.getSession().setAttribute("duplicatedList",duplicatedList);
This workaround does the trick (now I can read the bean “duplicatedList” from the session correctly) but it just doesn’t seem right that the original bean loses the ArrayList on some point and still maintain the other fields.
Thanks in advance
It seems that somewhere else in your application, some code is modifying the
List(since you said that it’s notnullbut empty – if it wasnullI’d expect that it was removed from context at all by some other part of code). Maybe after puttingListto the context some code still maintains reference and operates on it?You can try to do the following: