Page 1 of 1

Why am I getting a Null pointer exception when using xpath?

PostPosted: Tue Aug 20, 2013 9:48 pm
by zzzimon
Hi, I'm tring to use xpath in my code:

Code: Select all
   
public static void main(String[] args) throws Exception {
      WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
      MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
            
      StyleDefinitionsPart sdp = new StyleDefinitionsPart();
      sdp.unmarshalDefaultStyles();
      mdp.addTargetPart(sdp);
      
      Styles styles = sdp.getJaxbElement();
      java.util.List<Object> nodeList = mdp.getJAXBNodesViaXPath("//styles/style",styles,false);
      for (int i = 0; i < nodeList.size(); i++) {         
         Style style = (Style) nodeList.get(i);
         String styleId = style.getStyleId();
         System.out.println(styleId);
      }
}


What the code is suppose to do is create a StylesDefinitionsPart, then populate it with defalut styles, then find all the style elements and print their style id. Why is it not working and giving me a null pointer exception ? The exception happens on the line with the call to getJAXBNodesViaXPath.

/Simon

Re: Why am I getting a Null pointer exception when using xpa

PostPosted: Tue Aug 20, 2013 11:33 pm
by jason
StyleDefinitionsPart sdp = new StyleDefinitionsPart() wouldn't set the JaxbElement to anything, so when you do sdp.getJaxbElement(), it returns null.

So you need to do Styles styles = new Styles(); sdp.setJaxbElement(styles);

But, doing that wouldn't be enough to create the binder which is necessary for XPath to work. This issue is fixed on GitHub, by https://github.com/plutext/docx4j/commi ... 264485978e of August 7, so a more recent nightly ought to be fine.

Re: Why am I getting a Null pointer exception when using xpa

PostPosted: Wed Aug 21, 2013 11:21 pm
by zzzimon
Hi, thanks for your quick reply. I'm trying to do what you said, see below, but I still get the null pointer exception.

Code: Select all
   public static void main(String[] args) throws Exception {
      WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
      MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
            
      StyleDefinitionsPart sdp = new StyleDefinitionsPart();
      Styles styles = new Styles();
      sdp.setJaxbElement(styles);
      sdp.unmarshalDefaultStyles();
      mdp.addTargetPart(sdp);
      
      java.util.List<Object> nodeList = mdp.getJAXBNodesViaXPath("//styles/style",styles,false);
      for (int i = 0; i < nodeList.size(); i++) {         
         Style style = (Style) nodeList.get(i);
         String styleId = style.getStyleId();
         System.out.println(styleId);
      }
   }


I went to http://www.docx4java.org/docx4j/ and clicked on the link docx4j-nightly-20130821.jar. I then opened the downloaded item and took the file "docx4j-nightly-20130821.jar" and used it to replace the old docx4j file on my classpath. Do you think the null pointer exception is because of my code above, or did I download and use the wrong files?

/Simon

Re: Why am I getting a Null pointer exception when using xpa

PostPosted: Thu Aug 22, 2013 8:53 am
by jason
What is the stack trace for the NPE?

mdp.getJAXBNodesViaXPath("//styles/style",styles,false); is no good.

For a start, you want sdp.getJAXBNodesViaXPath

Secondly, you've left off the namespace prefix. You want something like //w:styles/w:style

Try sdp.getJAXBNodesViaXPath("//w:styles/w:style", false);

Re: Why am I getting a Null pointer exception when using xpa

PostPosted: Fri Aug 23, 2013 8:03 pm
by zzzimon
Hi, thanks for helping me again. I updated the code with what you said, see below. It compiles fine but when run it generates a "NoSuchMethodError: org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart.getJAXBNodesViaXPath(Ljava/lang/String;Z)Ljava/util/List;"

Code: Select all
   public static void main(String[] args) throws Exception {
      WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
      MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
            
      StyleDefinitionsPart sdp = new StyleDefinitionsPart();
      Styles styles = new Styles();
      sdp.setJaxbElement(styles);
      sdp.unmarshalDefaultStyles();
      mdp.addTargetPart(sdp);
      
      java.util.List<Object> nodeList = sdp.getJAXBNodesViaXPath("//w:styles/w:style",false);
      for (int i = 0; i < nodeList.size(); i++) {         
         Style style = (Style) nodeList.get(i);
         String styleId = style.getStyleId();
         System.out.println(styleId);
      }


Ive looked at the javadoc and there is no mention of a getJAXBNodesViaXPath method in the StyleDefinitionsPart class. Could this be whats causing the error? But then it shouldn't have compiled I'm thinking.

/Simon

Re: Why am I getting a Null pointer exception when using xpa

PostPosted: Fri Aug 23, 2013 8:52 pm
by jason
Seems like you might be using different versions of docx4j at compile time versus runtime? Better double check that...

Re: Why am I getting a Null pointer exception when using xpa

PostPosted: Fri Aug 23, 2013 10:40 pm
by zzzimon
Hi, thanks again. Like you said, I think I had an old jar file still on the classpath so I removed it. Now I get a slightly different error:

"NoClassDefFoundError: org/slf4j/LoggerFactory at org.docx4j.openpackaging.Base.<clinit>(Base.java:43)

The exception now happens on the very first line of the program:
Code: Select all
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();


Something must be very fundamentally wrong. Any ideas?

/Simon

Re: Why am I getting a Null pointer exception when using xpa

PostPosted: Fri Aug 23, 2013 10:52 pm
by jason
The nightly you are using is what will eventually become 3.0, and it uses slf4j, instead of log4j.

You just need to add:

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>

Re: Why am I getting a Null pointer exception when using xpa

PostPosted: Tue Aug 27, 2013 8:19 pm
by zzzimon
Hi thank you for reply again.
Where should I put those lines you just wrote?

/Simon

Re: Why am I getting a Null pointer exception when using xpa

PostPosted: Tue Aug 27, 2013 8:34 pm
by jason
That's just maven syntax to say what you need.

If you are not using maven, download the relevant jar from http://repo2.maven.org/maven2/org/slf4j ... api/1.7.5/ or http://www.slf4j.org/download.html