java - How to track an object inside an ArrayList becoming null? -
the structure have is:
map<string, arraylist<bean>>
this arraylist modified (added/removed) different locations within different threads.
at times, bean inside arraylist
becoming null
. how can track when becomes null? want track makes bean null, fix bug.
this happens when scenario tested huge data set.
here's exact code that's failing:
for (int = 0; < eventlogs.size(); i++) { messageeventlogbean msgeventlogbean = (messageeventlogbean) eventlogs.get(i); eventlogs.remove(i); try { logwriter.write(msgeventlogbean, context); } catch (fusionexception e) { logger.error("error while writing event log bean ", e); } }
instead of standard list implementation, use anonymous class overrides add
method , adds special code check if added object null, this:
list<t> list = new arraylist<t>() { public boolean add(t e) { if (e == null) { throw new nullpointerexception("attempt add null list"); } return super.add(e); } };
you should override "add" methods sure. when code adds null, explode , see exception in log , able see did examining stacktrace.
edited
to clear, impossible object in list "become null
". can add null
, or can remove object list, object already in list stay there until removed.
to clear, only way null
in list putting there - ie adding via 1 of add()
methods or addall()
method. concurrent modification issues can not cause issue (you may concurrentmodificationexception
, still won't put null
in there).
code such as
object o = list.get(1); o = null;
has no effect because you're nulling copy of reference object - list
still reference object.
however, depending on design of bean
objects, might mutable. while reference bean object remain intact, might possible of fields within bean become null
. catch this, need code setters explode when given null
argument, either re-writing class, or overloading setters via anonymous class (similar initial suggestion above).
Comments
Post a Comment