AlexRausch
AlexRausch

Reputation: 13

NullPointerException in static init block when WAR build/deploy to WildFly via Maven

i am trying to build and deploy a WAR to JBoss via maven. When im build/deploy via IntellJ everything works fine.

Deployed via Maven i get this Stacktrace:

018-02-20 16:59:09,863 ERROR [io.undertow.request] (default task-3) UT005023: Exception handling request to /akkredit-1/akkredit:  java.lang.ExceptionInInitializerError
at de.akkredit.Akkreditor$3.<init>(Akkreditor.java:109)
at de.akkredit.Akkreditor.getModules(Akkreditor.java:106)
at de.akkredit.Akkreditor.akkredit(Akkreditor.java:135)
at de.akkredit.service.Servlet.doPost(Servlet.java:48)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
....
Caused by: java.lang.NullPointerException
at de.akkredit.tools.Tools.<clinit>(Tools.java:41)
... 44 more

Tools is a Class with static functions and static initializing block:

    try {
        String xslt = Tools.class.getClassLoader().getResource("/transform.xslt").getFile();
        xsltTransformer = tf.newTransformer( new StreamSource( new File( xslt ) ) );
        <line 41:> xsltTransformer.setOutputProperty( OutputKeys.INDENT, "yes" );
        xsltTransformer.setOutputProperty( "{http://xml.apache.org/xslt}indent-amount", "2" );
        xsltTransformer.setOutputProperty( OutputKeys.ENCODING,"UTF-8" );
        xsltTransformer.setOutputProperty( OutputKeys.OMIT_XML_DECLARATION, "yes" );
    } catch (TransformerConfigurationException e1) { e1.printStackTrace(); }

See line41

I do think that happens because the resource transform.xslt was not found

The file is located in src/main/resources

in the WAR: WEB-INF/classes/

would appreciate if anyone could give me a hint here.

Regards

Alex

Upvotes: 1

Views: 104

Answers (1)

Lothar
Lothar

Reputation: 5449

    String xslt = Tools.class.getClassLoader().getResource("/transform.xslt").getFile();
    xsltTransformer = tf.newTransformer( new StreamSource( new File( xslt ) ) );

If the resource resides in the WAR-file, the returned resource will be something like C:/some/dir/to/the/webapp/WebApp.war!/transform.xslt that can't be opened with java.io.File.

Use getResourceAsStream instead and pass the returned InputStream to your StreamSource. The method returns null if the resource can't be found, so might check that and log that instead of letting the whole thing crash again with a NullPointerException. If that happens, you might try changing the resource to look for, e.g. to transform.xslt (i.e. without the leading slash) or really make sure that the resource is actually in the war-file where it's supposed to be.

BTW: You have mentioned "line 41" but I don't see any information in your code what line 41 actually is, so I assumed that

  1. undeployed, i.e. residing in a directory on your test system, everything works as expected
  2. That line 41 is actually the line with new StreamSource( new File( xslt ) ) and the NullPointerException comes from there.

BTW2: You said

The file is located in src/main/resources

in the WAR: WEB-INF/classes/

Does that mean that your resource resides in the directory src/main/resources that itself is located in WEB-INF/classes in your war-file? In that case you need to change the resource to look for to /src/main/resources/transform.xslt. If you meant that it's residing in the former directory while you do your tests and it resides in the latter when deployed as WAR, never mind ;-)

Upvotes: 1

Related Questions