title: Getting Exception when reorder RowLabel when hiding the Subtotal -Apache POI
body: <p>As per below code i dont want to add addRowLabel(1) but need addRowLabel(2) . After running the application and open the pivot table its giving exception but if you add addRowLabel(1)(currently commented) its working as expected. This is happening after adding the logic to hide Subtotal. Is this is the expected behaviour of apache POI or It can be fixed?
    Please find the  code below. 
  Note: This issue comes when hiding subtotal. </p>
  
  <pre><code>  package com.test.pivottables;
    import org.apache.poi.xssf.usermodel.*;
    import org.apache.poi.ss.usermodel.*;
     import org.apache.poi.ss.util.*;
       import org.apache.poi.ss.*;
     import java.io.*;
     import java.util.ArrayList;
    import java.util.HashSet;
     import java.util.Iterator;
   import java.util.List;
   import java.util.Set;
    import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
   class TestPivotTables {
    public static void main(String[] args) throws IOException{
      Workbook wb = new XSSFWorkbook();
      String[][] data = new String[][]{{"STATUS","PASSED","DATA","VALUE"}, 
    {"BLUE","Y","TTT","20"},
   {"RED","N","UUU","10"},{"BLUE","N","PPP","30"}};
  
      XSSFSheet sheet = (XSSFSheet) wb.createSheet("data");
      XSSFSheet pivot = (XSSFSheet) wb.createSheet("summary");
      for(String[] dataRow : data){
          XSSFRow row = sheet.createRow(sheet.getPhysicalNumberOfRows());
          for(String dataCell : dataRow){
              XSSFCell cell = 
          row.createCell(row.getPhysicalNumberOfCells());
              cell.setCellValue(dataCell);
          }
      }
  
      XSSFTable table = sheet.createTable();    
      CTTable cttable = table.getCTTable();
      table.setDisplayName("table");
      cttable.setRef("A1:D4");
      cttable.setId(1);
  
      CTTableColumns columns = cttable.addNewTableColumns();
      columns.setCount(3);
  
      int i = 1;
      for (String colName : data[0]){
          CTTableColumn column = columns.addNewTableColumn();
          column.setId(++i);
          column.setName(colName);      
      }
  
      XSSFPivotTable pivotTable =  pivot.createPivotTable(new 
   AreaReference("A1:D4", SpreadsheetVersion.EXCEL2007),
  new CellReference("A4"), sheet);
  
      pivotTable.addRowLabel(0);
      pivotTable.addRowLabel(2);
      //pivotTable.addRowLabel(1);
  
      List&lt;Integer&gt; iterList = new ArrayList&lt;Integer&gt;();
      iterList.add(0);
      iterList.add(2);
      //iterList.add(1);
      pivotTable.getCTPivotTableDefinition().setRowHeaderCaption("Colour");
     for (int j=0;j&lt;iterList.size();j++) {
          CTPivotField ctPivotField = 
     pivotTable.getCTPivotTableDefinition().getPivotFields().
    getPivotFieldList().get(iterList.get(j));
          for (i = 0; i &lt; sheet.getLastRowNum()-1; i++) {
              if(ctPivotField.getItems().getItemArray(i)!=null) {
                  ctPivotField.getItems().getItemArray(i).unsetT();
                  ctPivotField.getItems().getItemArray(i).setX((long)i);
              }
          }
          for (i = sheet.getLastRowNum(); i &gt; sheet.getLastRowNum()-2; i--) 
    {
              if(ctPivotField.getItems().getItemArray(i)!=null) {
                  ctPivotField.getItems().removeItem(i);
              }
          }
          ctPivotField.getItems().setCount(2);
  
          Set&lt;String&gt; collection = new HashSet&lt;String&gt;();
          int ctr = 0;
          Row row = null;
          Cell cell = null;
          boolean isNull = false;
          do{
              try{
                  row = sheet.getRow(ctr);
                  cell = row.getCell(0);
                  collection.add(cell.toString());
                  ctr++;
              } catch(Exception e) {
                  isNull = true;
              }
          }while(isNull!=true);
          if(collection!=null &amp;&amp; collection.size()&gt;0) {
              Iterator value = collection.iterator(); 
              while (value.hasNext()) { 
                  pivotTable.getPivotCacheDefinition().
    getCTPivotCacheDefinition().getCacheFields().
    getCacheFieldList().get(j).getSharedItems().addNewS().
    setV(value.next().toString()); 
              } 
          }
          ctPivotField.setAutoShow(false);
          ctPivotField.setDefaultSubtotal(false);
          ctPivotField.setSubtotalTop(false);
          ctPivotField.setSubtotalCaption(null);
          ctPivotField.setCompact(false);
      }
      System.out.println("----end---");
      pivotTable.addColumnLabel(DataConsolidateFunction.COUNT, 3, "test");
  
      FileOutputStream fileOut = new FileOutputStream("pivotsample1.xlsx");
      wb.write(fileOut);
      wb.close();
  
  }}
  </code></pre>