You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by kgtokm <kg...@acmoore.com> on 2014/08/29 23:09:08 UTC
POI 3.11 NullPointerException when doing removeSheetAT() for .xls
file
Hi,
My .xls workbook contains several sheets and when removing sheet using
removeSheetAt(index) method it throws NullPointerException.
For eg if I have have 3 sheets in my workboox and if I remove 3nd and then
2nd sheet it works fine but if I remove 1st sheet or 2nd sheet by keeping
3rd sheet then it throws NullPointerException
Iam using poi 3.11 version
Any help would be appreciated.
Thanks in advance
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/POI-3-11-NullPointerException-when-doing-removeSheetAT-for-xls-file-tp5716487.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org
Re: POI 3.11 NullPointerException when doing removeSheetAT() for .xls file
Posted by Dominik Stadler <do...@gmx.at>.
Hi,
With a random sample xls I do not see the NullPointerException.
Can you also share the XLS file that is triggering this? Or even
better a self-contained unit-test that does not require one to adjust
the code to make it compile?
Also the full stacktrace of the exception would help in finding a
possible cause.
Thanks... Dominik.
On Wed, Sep 3, 2014 at 5:04 PM, kgtokm <kg...@acmoore.com> wrote:
> Hi Adrian Lynch ,
>
> I posted the below code from the site and I wasn't sure you have received it or not so I am sending it again
>
> Below is my code for fetching and deleting all the tabs that are not required.
>
> Workbook wb=null;
> Sheet sheet=null;
> for (int a = 0; a < sheetCount; a++) {
> inStream = new FileInputStream(txtFile.getText().trim());
> if(txtFile.getText().trim().substring(txtFile.getText().trim().indexOf(".") + 1).equalsIgnoreCase("xls"))
> wb = new HSSFWorkbook(inStream);
> else
> wb = new XSSFWorkbook(inStream);
> workingSheetName = wb.getSheetName(a);
>
> //get all other tabs to delete
> for (int b = 0; b < wb.getNumberOfSheets(); b++) {
> if (!wb.getSheetName(b).trim().equalsIgnoreCase(workingSheetName.trim()))
> tabsToDelete.add(wb.getSheetName(b));
> }
>
> //now delete those tabs
> for (int c = 0; c < tabsToDelete.size(); c++) {
> if (tabsToDelete.get(c) != null)
> wb.removeSheetAt(wb.getSheetIndex((String) tabsToDelete.get(c))); //This is where my application fails
> }
> }
>
>
> From: Adrian Lynch [via Apache POI] [mailto:ml-node+s1045710n5716488h50@n5.nabble.com]
> Sent: Friday, August 29, 2014 6:21 PM
> To: Kranthi Garneni
> Subject: Re: POI 3.11 NullPointerException when doing removeSheetAT() for .xls file
>
> Show us your code. It's hard to even suggest what might be the issue
> without some code to look over.
>
> A
>
>
> On 29 August 2014 22:09, kgtokm <[hidden email]</user/SendEmail.jtp?type=node&node=5716488&i=0>> wrote:
>
>> Hi,
>>
>> My .xls workbook contains several sheets and when removing sheet using
>> removeSheetAt(index) method it throws NullPointerException.
>>
>> For eg if I have have 3 sheets in my workboox and if I remove 3nd and then
>> 2nd sheet it works fine but if I remove 1st sheet or 2nd sheet by keeping
>> 3rd sheet then it throws NullPointerException
>>
>> Iam using poi 3.11 version
>>
>> Any help would be appreciated.
>>
>> Thanks in advance
>>
>>
>>
>> --
>> View this message in context:
>> http://apache-poi.1045710.n5.nabble.com/POI-3-11-NullPointerException-when-doing-removeSheetAT-for-xls-file-tp5716487.html
>> Sent from the POI - User mailing list archive at Nabble.com.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]</user/SendEmail.jtp?type=node&node=5716488&i=1>
>> For additional commands, e-mail: [hidden email]</user/SendEmail.jtp?type=node&node=5716488&i=2>
>>
>>
>
> ________________________________
> If you reply to this email, your message will be added to the discussion below:
> http://apache-poi.1045710.n5.nabble.com/POI-3-11-NullPointerException-when-doing-removeSheetAT-for-xls-file-tp5716487p5716488.html
> To unsubscribe from POI 3.11 NullPointerException when doing removeSheetAT() for .xls file, click here<http://apache-poi.1045710.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=5716487&code=a2dhcm5lbmlAYWNtb29yZS5jb218NTcxNjQ4N3wtMjA5NjY0Mzcy>.
> NAML<http://apache-poi.1045710.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>
> ________________________________
> Notice: This message and any attachments may contain confidential or proprietary information and are only for the use of the intended recipient of this message. If you are not the intended recipient, please notify the sender by return email, and delete or destroy this and all copies of this message and all attachments. Any unauthorized disclosure, use, distribution or reproduction of this message or any attachments is prohibited and may be unlawful.
>
>
>
>
> --
> View this message in context: http://apache-poi.1045710.n5.nabble.com/POI-3-11-NullPointerException-when-doing-removeSheetAT-for-xls-file-tp5716487p5716526.html
> Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org
RE: POI 3.11 NullPointerException when doing removeSheetAT() for
.xls file
Posted by kgtokm <kg...@acmoore.com>.
Hi Adrian Lynch ,
I posted the below code from the site and I wasn't sure you have received it or not so I am sending it again
Below is my code for fetching and deleting all the tabs that are not required.
Workbook wb=null;
Sheet sheet=null;
for (int a = 0; a < sheetCount; a++) {
inStream = new FileInputStream(txtFile.getText().trim());
if(txtFile.getText().trim().substring(txtFile.getText().trim().indexOf(".") + 1).equalsIgnoreCase("xls"))
wb = new HSSFWorkbook(inStream);
else
wb = new XSSFWorkbook(inStream);
workingSheetName = wb.getSheetName(a);
//get all other tabs to delete
for (int b = 0; b < wb.getNumberOfSheets(); b++) {
if (!wb.getSheetName(b).trim().equalsIgnoreCase(workingSheetName.trim()))
tabsToDelete.add(wb.getSheetName(b));
}
//now delete those tabs
for (int c = 0; c < tabsToDelete.size(); c++) {
if (tabsToDelete.get(c) != null)
wb.removeSheetAt(wb.getSheetIndex((String) tabsToDelete.get(c))); //This is where my application fails
}
}
From: Adrian Lynch [via Apache POI] [mailto:ml-node+s1045710n5716488h50@n5.nabble.com]
Sent: Friday, August 29, 2014 6:21 PM
To: Kranthi Garneni
Subject: Re: POI 3.11 NullPointerException when doing removeSheetAT() for .xls file
Show us your code. It's hard to even suggest what might be the issue
without some code to look over.
A
On 29 August 2014 22:09, kgtokm <[hidden email]</user/SendEmail.jtp?type=node&node=5716488&i=0>> wrote:
> Hi,
>
> My .xls workbook contains several sheets and when removing sheet using
> removeSheetAt(index) method it throws NullPointerException.
>
> For eg if I have have 3 sheets in my workboox and if I remove 3nd and then
> 2nd sheet it works fine but if I remove 1st sheet or 2nd sheet by keeping
> 3rd sheet then it throws NullPointerException
>
> Iam using poi 3.11 version
>
> Any help would be appreciated.
>
> Thanks in advance
>
>
>
> --
> View this message in context:
> http://apache-poi.1045710.n5.nabble.com/POI-3-11-NullPointerException-when-doing-removeSheetAT-for-xls-file-tp5716487.html
> Sent from the POI - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]</user/SendEmail.jtp?type=node&node=5716488&i=1>
> For additional commands, e-mail: [hidden email]</user/SendEmail.jtp?type=node&node=5716488&i=2>
>
>
________________________________
If you reply to this email, your message will be added to the discussion below:
http://apache-poi.1045710.n5.nabble.com/POI-3-11-NullPointerException-when-doing-removeSheetAT-for-xls-file-tp5716487p5716488.html
To unsubscribe from POI 3.11 NullPointerException when doing removeSheetAT() for .xls file, click here<http://apache-poi.1045710.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=5716487&code=a2dhcm5lbmlAYWNtb29yZS5jb218NTcxNjQ4N3wtMjA5NjY0Mzcy>.
NAML<http://apache-poi.1045710.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
________________________________
Notice: This message and any attachments may contain confidential or proprietary information and are only for the use of the intended recipient of this message. If you are not the intended recipient, please notify the sender by return email, and delete or destroy this and all copies of this message and all attachments. Any unauthorized disclosure, use, distribution or reproduction of this message or any attachments is prohibited and may be unlawful.
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/POI-3-11-NullPointerException-when-doing-removeSheetAT-for-xls-file-tp5716487p5716526.html
Sent from the POI - User mailing list archive at Nabble.com.
Re: POI 3.11 NullPointerException when doing removeSheetAT() for
.xls file
Posted by kgtokm <kg...@acmoore.com>.
Hi Adrian Lynch
Below is my code for fetching and deleting all the tabs that are not
required.
Workbook wb=null;
Sheet sheet=null;
for (int a = 0; a < sheetCount; a++) {
inStream = new FileInputStream(txtFile.getText().trim());
if(txtFile.getText().trim().substring(txtFile.getText().trim().indexOf(".")
+ 1).equalsIgnoreCase("xls"))
wb = new HSSFWorkbook(inStream);
else
wb = new XSSFWorkbook(inStream);
workingSheetName = wb.getSheetName(a);
//get all other tabs to delete
for (int b = 0; b < wb.getNumberOfSheets(); b++) {
if
(!wb.getSheetName(b).trim().equalsIgnoreCase(workingSheetName.trim()))
tabsToDelete.add(wb.getSheetName(b));
}
//now delete those tabs
for (int c = 0; c < tabsToDelete.size(); c++) {
if (tabsToDelete.get(c) != null)
wb.removeSheetAt(wb.getSheetIndex((String)
tabsToDelete.get(c))); //This is where my application fails
}
}
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/POI-3-11-NullPointerException-when-doing-removeSheetAT-for-xls-file-tp5716487p5716521.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org
Re: POI 3.11 NullPointerException when doing removeSheetAT() for
.xls file
Posted by kgtokm <kg...@acmoore.com>.
Hi all,
Thanks for the reply
Adrian Lynch
I will send my code tomorrow as I don't have access to the code right now
Tohnmeister
I have a for loop to remove the sheets which loops from 0 to number of sheets to be removed.
If I have to remove sheet 0 , it fails at the 1st attempt itself. I believe this might be related to indexes but I couldn't figure it out
Please note that the same code works for .xlsx
Thanks
On Sep 1, 2014, at 10:50 AM, "Tohnmeister [via Apache POI]" <ml...@n5.nabble.com>> wrote:
Note that indexes are updated when you delete a sheet. E.g. at first you
have three sheets, namely 0, 1 and 2. After removing 0, you have two
sheets, namely 0 (was 1) and 1 (was 2). If your code looks like this:
workbook.removeSheetAt(0);
workbook.removeSheetAt(2);
Then that will fail indeed, because index 2 is not valid anymore after
removing sheet 0.
2014-08-30 0:20 GMT+02:00 Adrian Lynch <[hidden email]</user/SendEmail.jtp?type=node&node=5716517&i=0>>:
> Show us your code. It's hard to even suggest what might be the issue
> without some code to look over.
>
> A
>
>
> On 29 August 2014 22:09, kgtokm <[hidden email]</user/SendEmail.jtp?type=node&node=5716517&i=1>> wrote:
>
> > Hi,
> >
> > My .xls workbook contains several sheets and when removing sheet using
> > removeSheetAt(index) method it throws NullPointerException.
> >
> > For eg if I have have 3 sheets in my workboox and if I remove 3nd and
> then
> > 2nd sheet it works fine but if I remove 1st sheet or 2nd sheet by keeping
> > 3rd sheet then it throws NullPointerException
> >
> > Iam using poi 3.11 version
> >
> > Any help would be appreciated.
> >
> > Thanks in advance
> >
> >
> >
> > --
> > View this message in context:
> >
> http://apache-poi.1045710.n5.nabble.com/POI-3-11-NullPointerException-when-doing-removeSheetAT-for-xls-file-tp5716487.html
> > Sent from the POI - User mailing list archive at Nabble.com<http://Nabble.com>.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [hidden email]</user/SendEmail.jtp?type=node&node=5716517&i=2>
> > For additional commands, e-mail: [hidden email]</user/SendEmail.jtp?type=node&node=5716517&i=3>
> >
> >
>
________________________________
If you reply to this email, your message will be added to the discussion below:
http://apache-poi.1045710.n5.nabble.com/POI-3-11-NullPointerException-when-doing-removeSheetAT-for-xls-file-tp5716487p5716517.html
To unsubscribe from POI 3.11 NullPointerException when doing removeSheetAT() for .xls file, click here<http://apache-poi.1045710.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=5716487&code=a2dhcm5lbmlAYWNtb29yZS5jb218NTcxNjQ4N3wtMjA5NjY0Mzcy>.
NAML<http://apache-poi.1045710.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
________________________________
Notice: This message and any attachments may contain confidential or proprietary information and are only for the use of the intended recipient of this message. If you are not the intended recipient, please notify the sender by return email, and delete or destroy this and all copies of this message and all attachments. Any unauthorized disclosure, use, distribution or reproduction of this message or any attachments is prohibited and may be unlawful.
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/POI-3-11-NullPointerException-when-doing-removeSheetAT-for-xls-file-tp5716487p5716518.html
Sent from the POI - User mailing list archive at Nabble.com.
Re: POI 3.11 NullPointerException when doing removeSheetAT() for .xls file
Posted by Tonni Tielens <to...@gmail.com>.
Note that indexes are updated when you delete a sheet. E.g. at first you
have three sheets, namely 0, 1 and 2. After removing 0, you have two
sheets, namely 0 (was 1) and 1 (was 2). If your code looks like this:
workbook.removeSheetAt(0);
workbook.removeSheetAt(2);
Then that will fail indeed, because index 2 is not valid anymore after
removing sheet 0.
2014-08-30 0:20 GMT+02:00 Adrian Lynch <ad...@concreteplatform.com>:
> Show us your code. It's hard to even suggest what might be the issue
> without some code to look over.
>
> A
>
>
> On 29 August 2014 22:09, kgtokm <kg...@acmoore.com> wrote:
>
> > Hi,
> >
> > My .xls workbook contains several sheets and when removing sheet using
> > removeSheetAt(index) method it throws NullPointerException.
> >
> > For eg if I have have 3 sheets in my workboox and if I remove 3nd and
> then
> > 2nd sheet it works fine but if I remove 1st sheet or 2nd sheet by keeping
> > 3rd sheet then it throws NullPointerException
> >
> > Iam using poi 3.11 version
> >
> > Any help would be appreciated.
> >
> > Thanks in advance
> >
> >
> >
> > --
> > View this message in context:
> >
> http://apache-poi.1045710.n5.nabble.com/POI-3-11-NullPointerException-when-doing-removeSheetAT-for-xls-file-tp5716487.html
> > Sent from the POI - User mailing list archive at Nabble.com.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> > For additional commands, e-mail: user-help@poi.apache.org
> >
> >
>
Re: POI 3.11 NullPointerException when doing removeSheetAT() for .xls file
Posted by Adrian Lynch <ad...@concreteplatform.com>.
Show us your code. It's hard to even suggest what might be the issue
without some code to look over.
A
On 29 August 2014 22:09, kgtokm <kg...@acmoore.com> wrote:
> Hi,
>
> My .xls workbook contains several sheets and when removing sheet using
> removeSheetAt(index) method it throws NullPointerException.
>
> For eg if I have have 3 sheets in my workboox and if I remove 3nd and then
> 2nd sheet it works fine but if I remove 1st sheet or 2nd sheet by keeping
> 3rd sheet then it throws NullPointerException
>
> Iam using poi 3.11 version
>
> Any help would be appreciated.
>
> Thanks in advance
>
>
>
> --
> View this message in context:
> http://apache-poi.1045710.n5.nabble.com/POI-3-11-NullPointerException-when-doing-removeSheetAT-for-xls-file-tp5716487.html
> Sent from the POI - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>