Runaway index
Hi,
This is the strangest thing I've ever seen. I noticed it during debugging.
I noticed that the index was increased by one for every line I stepped with the debugger.
Can you explain to me how the output from running this method can be
3
6
8
10
The object context is a ServletContext instance set as a instance variable in the constructor.
This isn't a Servlet.
There is only one thread running the code ( I'm doing this in my testing environment and I'm the only one posting any requests against the Servlet that creates the class with this method ).
public DaysOfMonth getNext(){
DaysOfMonth daysOfMonth =null;
int index = 1;
// Synchronize access to the context since it isn't thread safe.
synchronized(context){
System.err.println(index);
// Get the index, of the next daysOfMonth to use, from ServletContext.
// If no index is stored, use index 1 ( start index ).
Object objIndex = context.getAttribute(GTA_SEARCH_DAYSOFMONTH_INDEX);
if (objIndex !=null && objIndexinstanceof Integer){
// By the way, objIndex is assigned null so this part of the code isn't run.
index = ((Integer) objIndex).intValue();
}
System.err.println(index);
boolean cont =false;
do{
System.err.println(index);
cont =false;
System.err.println(index);
// Get the next daysOfMonth.
String daysOfMonthString = PropertyHandler.GetInstance().getString(PROPERTY_FILE_NAME, GTA_SEARCH_DAYSOFMONTH + index,null);
// If no daysOfMonthString with that index...
if (daysOfMonthString ==null){
// ... and index is greater than 1 ( start index ), set index
// to start index and try retrieving the daysOfMonthString again.
if (index > 1){
index = 1;
cont =true;
notifyListeners();
}
// ... and index is 1 ( start index ) there are obviously no
// urls in the configuration file. Log the error and return null.
else{
Logger.getLogger(getClass().getName()).severe("No search daysOfMonth found in the configuration file. Tried to find " + GTA_SEARCH_DAYSOFMONTH + index);
}
}
// If the daysOfMonth is found...
else{
// ... increase the index by one and set it in the ServletContext.
context.setAttribute(GTA_SEARCH_DAYSOFMONTH_INDEX,new Integer(++index));
StringTokenizer tokenizer =new StringTokenizer(daysOfMonthString,"-");
daysOfMonth =new DaysOfMonthImpl(Integer.parseInt(tokenizer.nextToken()), Integer.parseInt(tokenizer.nextToken()));
}
}while (cont ==true);
}// end synchronization
return daysOfMonth;
}

