Note that there are some explanatory texts on larger screens.

plurals
  1. POIf and else statement at the same time though it shouldn't be possible
    primarykey
    data
    text
    <p>I'm experiencing a very weird problem in eclipse. Look at the following code:</p> <pre><code>public void addItem(ArrayList&lt;Object&gt; objectLists) { SHorizontalLayout hLayout = Cf.hLayout(); hLayout.setSizeFull(); hLayout.setHeight(rowHeight, UNITS_PIXELS); if(rowCount % 2 != 0 &amp;&amp; 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++; } </code></pre> <p>In the for loop the object is checked for instance type and added accordingly into a layout. </p> <p>The problem I'm experiencing though is that when the object is of type <code>Integer</code> it enters the <em>if statement</em>, 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 <code>columnList.get(1).addComponent(hLayout)</code> (skipping the first row in the else statement). </p> <p>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.</p> <p>If I'm to break down the problem into the smallest components:</p> <pre><code>i = 0; if(true) { i++; } else { i++; } System.out.println(i); </code></pre> <p>With my problem the printout would be: 2</p> <p>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?</p> <p><strong>EDIT:</strong> I've tried switching the statements around and can conclude that the pattern repeats itself.</p> <pre><code>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); } } </code></pre> <p><strong>EDIT 2:</strong> From Jon's request I added logging to the statements.</p> <pre><code>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); } } </code></pre> <p>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)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload