Hi Nicholas,
basically I do the following:
ClassLoader cl = getClass().getClassLoader();
URL url = cl.getResource("path/to/template.ods");
InputStream is = url.openStream();
SpreadsheetDocument document = SpreadsheetDocument.loadDocument(is);
Table template = this.document.getSheetByIndex(0);
document.appendSheet(template, "test1");
document.appendSheet(template, "test2");
At the last line of this code I get an NullPointerException as posted
before.
The template has one sheet and after saving the new created document with:
OutputStream os = FileOutputStream("path/to/file.ods");
document.save(os);
I have an exact copy of the template in the new document. But no new
sheets, and no sheets with the names "test1" and "test2".
Regards,
Philipp
On 04.11.2014 17:57, Nicholas Evans wrote:
> Hi Philipp,
>
> I have quickly tested the suggestion I just made and I see it working
> fine. How are you creating the document and the templatedocument? Are you
> sure the templatedocument actually has a sheet?
>
> Best,
> Nick
>
> 2014-11-04 15:26 GMT+01:00 Nicholas Evans <[email protected]>:
>
>> Hi Philipp,
>>
>> I have previously used the appendSheet("sheet") method without
>> difficulties, but have never tried to append an existing table. Does the
>> NullPointerException also occur if you try to append the sheet to the
>> template, as in
>> documenttemplate.appendSheet(documenttemplate.getSheetByIndex(0), "name") ?
>>
>> I will try and test the method when I have a minute.
>>
>> Best,
>> Nick
>>
>>
>> 2014-11-04 0:38 GMT+01:00 Philipp Thöricht <
>> [email protected]>:
>>
>>> Maybe now someone has a clue for me?
>>> As said in my previous mail, I tried to append a template sheet to a
>>> spreadsheet document. But the appendSheet method of the class
>>> SpreadsheetDocument gives me back the following error:
>>>
>>> java.lang.NullPointerException
>>> at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(Unknown Source)
>>> at sun.reflect.UnsafeQualifiedObjectFieldAccessorImpl.get(Unknown
>>> Source)
>>> at java.lang.reflect.Field.get(Unknown Source)
>>> at
>>> org.odftoolkit.odfdom.pkg.OdfFileDom.newOdfElement(OdfFileDom.java:340)
>>> at
>>> org.odftoolkit.odfdom.pkg.OdfElement.cloneNode(OdfElement.java:297)
>>> at
>>> org.odftoolkit.odfdom.pkg.OdfElement.cloneNode(OdfElement.java:316)
>>> at
>>>
>>> org.odftoolkit.simple.SpreadsheetDocument.appendSheet(SpreadsheetDocument.java:315)
>>>
>>> What I do is:
>>> document.appendSheet(documenttemplate.getSheetByIndex(0), "name");
>>> where document is the document I want to create and documenttemplate is
>>> the document with the template sheet.
>>> Surely I did something wrong, but I can't see what it is. I hope that
>>> someone can help me.
>>>
>>> Regards,
>>> Philipp
>>>
>>>
>>> On 01.10.2014 16:22, Philipp Thöricht wrote:
>>>> Hi,
>>>>
>>>> I'm searching for help how to handle templates with the odftoolkit.
>>>> I want to load a template document (spreadsheet), then create a new
>>>> spreadsheet document and add several sheets to it based on the sheet of
>>>> the template.
>>>> I tried methods like appendSheet(templatesheet, "name"),
>>>> insertSheet(templatesheet, 0) on the new created document, but they all
>>>> threw a NullPointerExceptions.
>>>>
>>>> Maybe someone has a code example how one can get this done?
>>>>
>>>> Regards,
>>>> Philipp
>>>>
>>>
>>
>>
>