javax.xml.ws Service NullPointerException when project is packaged in JAR file

24 Views Asked by At

I've created a client for a SOAP Web Service in Java 1.8 using the library javax.xml.ws.

A certain amount of code has been auto-generated using Apache CXF via SoapUI starting from the .wsdl and .xsd files.

Specifically this is the auto-generated class that procude the error

package it.sealink.datadriver.anpr.soap._3000anprservice;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
import javax.xml.namespace.QName;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceFeature;
import javax.xml.ws.Service;


@WebServiceClient(name = "AnprService3000",
                  wsdlLocation = "file:///C:/Users/fveglianti/Desktop/anpr-analyzer/src/main/resources/anpr-wsdl-8.1.5/wsdl3000/3000serviziConsultazione.wsdl",
                  targetNamespace = "http://fabianoNB:8088/mockAnprBinding3000")
public class AnprService3000 extends Service {

    private static final Logger log = LogManager.getLogger(AnprService3000.class);
    public final static URL WSDL_LOCATION;

    public final static QName SERVICE = new QName("http://fabianoNB:8088/mockAnprBinding3000", "AnprService3000");
    public final static QName AnprServicePort3000 = new QName("http://fabianoNB:8088/mockAnprBinding3000", "AnprServicePort3000");

    static {
        URL url = null;
        try {
            url = new URL("file:///C:/Users/fveglianti/Desktop/anpr-analyzer/src/main/resources/anpr-wsdl-8.1.5/wsdl3000/3000serviziConsultazione.wsdl");
            Scanner s = new Scanner(url.openStream());
        } catch (MalformedURLException e) {
            log.error(String.format("Can not initialize the default wsdl from [%s]", "file:///C:/Users/fveglianti/Desktop/anpr-analyzer/src/main/resources/anpr-wsdl-8.1.5/wsdl3000/3000serviziConsultazione.wsdl"));
        } catch (Exception e) {
            log.error(e.getClass().getName());
            log.error(e.getMessage());
            log.error(String.format("Can not open file at location [%s]", "file:///C:/Users/fveglianti/Desktop/anpr-analyzer/src/main/resources/anpr-wsdl-8.1.5/wsdl3000/3000serviziConsultazione.wsdl"));
        }
        WSDL_LOCATION = url;
        log.debug(String.format("wsdl location is [%s]", WSDL_LOCATION));
    }

    public AnprService3000(URL wsdlLocation) {
        super(wsdlLocation, SERVICE);
    }

    public AnprService3000(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public AnprService3000() {
        super(WSDL_LOCATION, SERVICE);
    }

    public AnprService3000(WebServiceFeature ... features) {
        super(WSDL_LOCATION, SERVICE, features);
    }

    public AnprService3000(URL wsdlLocation, WebServiceFeature ... features) {
        super(wsdlLocation, SERVICE, features);
    }

    public AnprService3000(URL wsdlLocation, QName serviceName, WebServiceFeature ... features) {
        super(wsdlLocation, serviceName, features);
    }
    

    /**
     *
     * @return
     *     returns AnprPortType3000
     */
    @WebEndpoint(name = "AnprServicePort3000")
    public AnprPortType3000 getAnprServicePort3000() {
        return super.getPort(AnprServicePort3000, AnprPortType3000.class);
    }

    /**
     *
     * @param features
     *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.
     * @return
     *     returns AnprPortType3000
     */
    @WebEndpoint(name = "AnprServicePort3000")
    public AnprPortType3000 getAnprServicePort3000(WebServiceFeature... features) {
        return super.getPort(AnprServicePort3000, AnprPortType3000.class, features);
    }

}

When I try to instantiate this class, so running the code

try {
        AnprService3000 port = new AnprService3000();
}catch (Exception e){
        log.error(e.getClass().getName());
        log.error(e.getMessage());
        log.error(e.getStackTrace());
}

a NullPointerException with message null is thrown.

The problem is that this behaviors only happens when I package my project (let's call it project A) into a JAR and I use it inside another project (let's say project B).

If I run project A's tests, all works correctly, but if I run project B's tests, which call the functionality of project A, I get the NullPointerException I described above.

Do you have any idea on how to solve this problem?

The only suspect I have is that the reference "file:///C:/Users/fveglianti/Desktop/anpr-analyzer/src/main/resources/anpr-wsdl-8.1.5/wsdl3000/3000serviziConsultazione.wsdl" fails, however, I put the instructions

url = new URL("file:///C:/Users/fveglianti/Desktop/anpr-analyzer/src/main/resources/anpr-wsdl-8.1.5/wsdl3000/3000serviziConsultazione.wsdl");
Scanner s = new Scanner(url.openStream());

in and they do not fail, so I don't think that is the problem.

This is the StackTrace

2023-12-15 12:56:12,243 ERROR 14356@fabianoNB (main) [it.sealink.datadriver.anpr.driver.DriverBase] - An unexpected NullPointerException occurred. Stack Trace:
2023-12-15 12:56:12,243 ERROR 14356@fabianoNB (main) [it.sealink.datadriver.anpr.driver.DriverBase] - org.apache.cxf.wsdl11.WSDLServiceFactory.<init>(WSDLServiceFactory.java:85)
2023-12-15 12:56:12,243 ERROR 14356@fabianoNB (main) [it.sealink.datadriver.anpr.driver.DriverBase] - org.apache.cxf.jaxws.ServiceImpl.initializePorts(ServiceImpl.java:218)
2023-12-15 12:56:12,259 ERROR 14356@fabianoNB (main) [it.sealink.datadriver.anpr.driver.DriverBase] - org.apache.cxf.jaxws.ServiceImpl.initialize(ServiceImpl.java:161)
2023-12-15 12:56:12,259 ERROR 14356@fabianoNB (main) [it.sealink.datadriver.anpr.driver.DriverBase] - org.apache.cxf.jaxws.ServiceImpl.<init>(ServiceImpl.java:129)
2023-12-15 12:56:12,259 ERROR 14356@fabianoNB (main) [it.sealink.datadriver.anpr.driver.DriverBase] - org.apache.cxf.jaxws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:81)
2023-12-15 12:56:12,259 ERROR 14356@fabianoNB (main) [it.sealink.datadriver.anpr.driver.DriverBase] - javax.xml.ws.Service.<init>(Service.java:77)
2023-12-15 12:56:12,259 ERROR 14356@fabianoNB (main) [it.sealink.datadriver.anpr.driver.DriverBase] - it.sealink.datadriver.anpr.soap._3000anprservice.AnprService3000.<init>(AnprService3000.java:60)
2023-12-15 12:56:12,259 ERROR 14356@fabianoNB (main) [it.sealink.datadriver.anpr.driver.DriverBase] - it.sealink.datadriver.anpr.driver.DriverBase.doSearch(DriverBase.java:51)
2023-12-15 12:56:12,259 ERROR 14356@fabianoNB (main) [it.sealink.datadriver.anpr.driver.DriverBase] - it.sealink.datadriver.anpr.ANPRAnalyzer.doSearch(ANPRAnalyzer.java:127)
2023-12-15 12:56:12,259 ERROR 14356@fabianoNB (main) [it.sealink.datadriver.anpr.driver.DriverBase] - it.sealink.datadriver.Program.main(Program.java:296)
0

There are 0 best solutions below