sheet.removeMergedRegion throwing Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

sheet.removeMergedRegion throwing Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

rdhanara
Hi,
trying to shift the merged region to the specific position on the right
side. So removing existing regions and creating the new merged region with
the new range address.

*Code:*
                   ArrayList<CellRangeAddress> regions = new
ArrayList<CellRangeAddress>();
                   int totalMergedRegions=sheet.getNumMergedRegions();
                   for (int i = 0; i < sheet.getNumMergedRegions(); i++){

                 CellRangeAddress region = sheet.getMergedRegion(i);
                 regions.add(region);
              }
                   for (int i = 0; i < totalMergedRegions; i++){
                           sheet.removeMergedRegion(i);
                      }
                     
                   for (CellRangeAddress region : regions){
                            CellRangeAddress newRegion = new CellRangeAddress(
                               region.getFirstRow(),
                               region.getLastRow() ,
                               region.getFirstColumn() + 9,
                               region.getLastColumn() + 9);
                            sheet.addMergedRegion(newRegion);
                         }

*Problem:*

Totally I have 12 merged regions available. trying to shift them 9 positions
to the right.
totalMergedRegions ==> 12
but ,  sheet.removeMergedRegion(i);
 giving index out of bound exception when i=6
*i.e.    sheet.removeMergedRegion(6);*

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array
index out of range: 5
        at
org.apache.poi.xssf.usermodel.XSSFSheet.removeMergedRegion(XSSFSheet.java:1433)

*********************************

Rest all the shifting happening fine. Kindly let me know the reason why
getting out of bound  at 6 when there are totally 12 positions available ?

Thanks In Advance ..!





--
Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: sheet.removeMergedRegion throwing Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

pj.fanning
If you remove regions inside the loop then the array gets smaller.



--
Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: sheet.removeMergedRegion throwing Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

rdhanara
But if you notice clearly

int totalMergedRegions=sheet.getNumMergedRegions();

  for (int i = 0; i < totalMergedRegions; i++){
                           sheet.removeMergedRegion(i);
        }

totalMergedRegions will not reduce hence array wont get smaller .

Any other suggestions please let me know .



Thanks...!



--
Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: sheet.removeMergedRegion throwing Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

kiwiwings
How about always removing index 0?

for (int i = 0; i < totalMergedRegions; i++){
    sheet.removeMergedRegion(0);
}



--
Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: sheet.removeMergedRegion throwing Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

rdhanara
Didn't get your point.

1.Do you want me to remove only index ==> 0 ? this case how can i remove
rest 11 merged regions..?

My understanding..
  Sheet has total 12 merged regions which has to be removed.
Example:
  Indexes : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 (total 12)

If we remove only 0 how can I remove rest of the positions?

Thanks...



--
Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: sheet.removeMergedRegion throwing Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

kiwiwings
rdhanara wrote
> 1.Do you want me to remove only index ==> 0 ? this case how can i remove
> rest 11 merged regions..?
>
> My understanding..
>   Sheet has total 12 merged regions which has to be removed.
> Example:
>   Indexes : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 (total 12)
>
> If we remove only 0 how can I remove rest of the positions?

- Start:    Indexes : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 (total 12)
- call removeMergedRegion(0)
- Result:  Indexes : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 (total 11)

2nd loop:
- Start:    Indexes : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 (total 11)
- call removeMergedRegion(0)
- Result:  Indexes : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (total 10)

As you already know - this is an index not an ID ... there's no index 11
after you have removed any of the merged entries.



--
Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: sheet.removeMergedRegion throwing Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

rdhanara
Ahh .. Got it .. Fixed that ..

Thanks for pointing it out ...:)



--
Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: sheet.removeMergedRegion throwing Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

Michal Stehlik
In reply to this post by rdhanara
Hi,

I believe problem is that as you remove regions, indices of remaining
regions shift - after removin 6 regions, you only have 6 remaining and thus
attempt to access 7th fails...

    Michal

On Wed, 15 May 2019, 13:08 rdhanara, <[hidden email]> wrote:

> Hi,
> trying to shift the merged region to the specific position on the right
> side. So removing existing regions and creating the new merged region with
> the new range address.
>
> *Code:*
>                    ArrayList<CellRangeAddress> regions = new
> ArrayList<CellRangeAddress>();
>                    int totalMergedRegions=sheet.getNumMergedRegions();
>                    for (int i = 0; i < sheet.getNumMergedRegions(); i++){
>
>                  CellRangeAddress region = sheet.getMergedRegion(i);
>                  regions.add(region);
>               }
>                    for (int i = 0; i < totalMergedRegions; i++){
>                            sheet.removeMergedRegion(i);
>                       }
>
>                    for (CellRangeAddress region : regions){
>                             CellRangeAddress newRegion = new
> CellRangeAddress(
>                                region.getFirstRow(),
>                                region.getLastRow() ,
>                                region.getFirstColumn() + 9,
>                                region.getLastColumn() + 9);
>                             sheet.addMergedRegion(newRegion);
>                          }
>
> *Problem:*
>
> Totally I have 12 merged regions available. trying to shift them 9
> positions
> to the right.
> totalMergedRegions ==> 12
> but ,  sheet.removeMergedRegion(i);
>  giving index out of bound exception when i=6
> *i.e.    sheet.removeMergedRegion(6);*
>
> Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array
> index out of range: 5
>         at
>
> org.apache.poi.xssf.usermodel.XSSFSheet.removeMergedRegion(XSSFSheet.java:1433)
>
> *********************************
>
> Rest all the shifting happening fine. Kindly let me know the reason why
> getting out of bound  at 6 when there are totally 12 positions available ?
>
> Thanks In Advance ..!
>
>
>
>
>
> --
> Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: sheet.removeMergedRegion throwing Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

rdhanara
Yeah. You are right. Fixed that already.

By the way thanks for the explanation .



--
Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]