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;

}

[4428 byte] By [mah01a] at [2007-10-2 12:51:49]
# 1

Actually this is getting stranger and stranger. I'm starting to suspect Eclipse for this. As soon as I renamed the variable index to e.g. indx or cnt the strange behaviour stoppes.

Also, it is only when I debug the code, stepping one line at a time that I get these faulty printouts. If I run the code without stopping the printouts are ok.

Anyway, changing the variable name fixed the problem.

mah01a at 2007-7-13 10:04:34 > top of Java-index,Java Essentials,Java Programming...
# 2
I cant use this StringTokenizer tokenizer = new StringTokenizer(daysOfMonthString, "-");with the link [option] like (daysOfMonthString, " http://www.retete-online.com"); or ["option1" - link1: http://www.retete-online.com/retete/cocktailuri/abbey-ro.php]
horea23a at 2007-7-13 10:04:34 > top of Java-index,Java Essentials,Java Programming...
# 3
I cant use this StringTokenizer tokenizer = new StringTokenizer(daysOfMonthString, "-");with the link [option] like (daysOfMonthString, " http://www.retete-online.com "); or ["option1" - link1: http://www.retete-online.com/retete/cocktailuri/abbey-ro.php]
horea23a at 2007-7-13 10:04:34 > top of Java-index,Java Essentials,Java Programming...
# 4
The dayOfMonth string is built up like this: 5-7 or 13-23 etc. It is the days in the month you want to stay at a hotel. But that part of the code has nothing to do with the problem I described :)
mah01a at 2007-7-13 10:04:34 > top of Java-index,Java Essentials,Java Programming...