[
https://issues.apache.org/jira/browse/ODFTOOLKIT-344?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13486777#comment-13486777
]
Marc Schreiber commented on ODFTOOLKIT-344:
-------------------------------------------
I've found a workaround and maybe the solution: The reason for the
NullPointException is located in OdfStylableElement.java line 116. This line
calls getAutomaticStyles() of OdfContentDom. But this line returns a null
reference because they are no automatik styles yet. I think in line 116 the
method getOrCreateAutomaticStyles() must be called (maybe the same counts for
the line 118 - I don't know).
This is my workaround (calling getOrCreateAutomaticStyles() by myself):
document.getContentDom().getOrCreateAutomaticStyles(); // create
automatic styles
TextNavigation navigation = new TextNavigation("Bundesnetzagentur",
document);
if (navigation.hasNext()) {
TextSelection selection = (TextSelection)
navigation.nextSelection();
Span span = Span.newSpan(selection);
DefaultStyleHandler style = span.getStyleHandler();
style.getTextPropertiesForWrite().setFontColor(Color.GREEN);
}
> NullPointerException in
> DefaultStyleHandler.getTextPropertiesForWrite().setFontColor()
> --------------------------------------------------------------------------------------
>
> Key: ODFTOOLKIT-344
> URL: https://issues.apache.org/jira/browse/ODFTOOLKIT-344
> Project: ODF Toolkit
> Issue Type: Bug
> Components: simple api
> Affects Versions: 0.7
> Reporter: Marc Schreiber
>
> I'm trying to change to font color of a certain word in a Open Document
> Textfile. I used the example for changing the style of a span from here:
> http://incubator.apache.org/odftoolkit/simple/document/cookbook/Text%20Document.html#Span
> This is my code:
> TextNavigation navigation = new TextNavigation("Word I'm looking
> for", document);
> if (navigation.hasNext()) {
> TextSelection selection = (TextSelection)
> navigation.nextSelection();
> Span span = Span.newSpan(selection);
> DefaultStyleHandler style = span.getStyleHandler();
> style.getTextPropertiesForWrite().setFontColor(Color.GREEN);
> }
> But the last line in the if block throws an NullPointerException. Here is the
> stack trace:
> Exception in thread "main" java.lang.NullPointerException
> at
> org.odftoolkit.simple.style.DefaultStyleHandler.getWritableStyleElementByName(DefaultStyleHandler.java:528)
> at
> org.odftoolkit.simple.style.DefaultStyleHandler.getStyleElementForWrite(DefaultStyleHandler.java:598)
> at
> org.odftoolkit.simple.style.DefaultStyleHandler.getTextPropertiesForWrite(DefaultStyleHandler.java:214)
> at
> etl.squared.informationextraction.Annotator.annotate(Annotator.java:124)
> at etl.squared.informationextraction.Annotator.main(Annotator.java:143)
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira