I'm experiencing a very weird problem in eclipse. Look at the following code:
public void addItem(ArrayList<Object> objectLists) {
SHorizontalLayout hLayout = Cf.hLayout();
hLayout.setSizeFull();
hLayout.setHeight(rowHeight, UNITS_PIXELS);
if(rowCount % 2 != 0 && rowCount != 0) {
hLayout.addStyleName("row-even");
} else {
hLayout.addStyleName("row-odd");
}
for(Object object : objectLists) {
if(object instanceof String || object instanceof Integer) {
hLayout.addComponent(Cf.h1(object.toString()), Alignment.MIDDLE_CENTER);
columnList.get(0).addComponent(hLayout);
} else if(object instanceof ChipSlotGrid) {
hLayout.addComponent((ChipSlotGrid)object, Alignment.MIDDLE_CENTER);
columnList.get(1).addComponent(hLayout);
}
}
rowCount++;
}
In the for loop the object is checked for instance type and added accordingly into a layout.
The problem I'm experiencing though is that when the object is of type Integer
it enters the if statement, executes the two rows inside the statement, and then instead of leaving for a new cycle in the loop it jumps into the else statement, executing row columnList.get(1).addComponent(hLayout)
(skipping the first row in the else statement).
It's executing parts of the else statement too even though it has already entered the if statement.I know this because I see the product of it in the application I'm developing and I've seen it programmatically when I'm debugging the code.
If I'm to break down the problem into the smallest components:
i = 0;
if(true) {
i++;
} else {
i++;
}
System.out.println(i);
With my problem the printout would be: 2
I'm at a loss here. Is there something wrong with my IDE? Has anyone encountered this before and have any idea of what could be wrong?
EDIT: I've tried switching the statements around and can conclude that the pattern repeats itself.
for(Object object : objectLists) {
if(object instanceof ChipSlotGrid) {
hLayout.addComponent((ChipSlotGrid)object, Alignment.MIDDLE_CENTER);
columnList.get(1).addComponent(hLayout);
} else if(object instanceof String || object instanceof Integer) {
hLayout.addComponent(Cf.h1(object.toString()), Alignment.MIDDLE_CENTER);
columnList.get(0).addComponent(hLayout);
}
}
EDIT 2: From Jon's request I added logging to the statements.
for(Object object : objectLists) {
if(object instanceof ChipSlotGrid) {
log.info("Inside if");
hLayout.addComponent((ChipSlotGrid)object, Alignment.MIDDLE_CENTER);
columnList.get(1).addComponent(hLayout);
} else if(object instanceof String || object instanceof Integer) {
log.info("Inside else");
hLayout.addComponent(Cf.h1(object.toString()), Alignment.MIDDLE_CENTER);
columnList.get(0).addComponent(hLayout);
}
}
When it jumps from the if statement to the else statement the log in the else statement is also skipped. (I hope this was the test you requested)
If your code looks like this:
if(object instanceof String || object instanceof Integer) {
hLayout.addComponent(Cf.h1(object.toString()), Alignment.MIDDLE_CENTER);
columnList.get(0).addComponent(hLayout);
} else if(object instanceof ChipSlotGrid)
hLayout.addComponent((ChipSlotGrid)object, Alignment.MIDDLE_CENTER);
columnList.get(1).addComponent(hLayout);
(note the lack of curly brackets on the else if
) then only the first line will be considered subject to the condition, and the second line will always be evaluated.
That's the only thing I can think of that would cause what you're explaining. Are you sure that the code you're looking at in your IDE is exactly what's being executed? If you have a class with the same package and name in more than one JAR, for example, then it may be using an outdated version of the class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With