2

Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate Constructor threw exception; nested exception is java.lang.NullPointerException

The exception is getting thrown in this class:

package com.GDSWebService.tripnomics_GDS_integration.connector.travelport;

import javax.xml.ws.BindingProvider;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.GDSWebService.tripnomics_GDS_integration.config.travelport.APIAccessConfig;
import com.GDSWebService.tripnomics_GDS_integration.modal.TravelportCredentials;
import com.GDSWebService.tripnomics_GDS_integration.service.HotelSearchImpl;
import com.GDSWebService.tripnomics_GDS_integration.utils.BindingUtil;
import com.travelport.schema.hotel_v48_0.BaseHotelSearchRsp;
import com.travelport.schema.hotel_v48_0.HotelSearchAvailabilityReq;
import com.travelport.service.hotel_v48_0.HotelSearchServicePortType;
import com.travelport.service.hotel_v48_0.HotelService;

@Component
public class HotelSearchConnector {

    @Autowired
    public APIAccessConfig apiAccessConfig;
    @Autowired
    public HotelSearchImpl hotelSearchImpl;

    public TravelportCredentials travelportCredentials= apiAccessConfig.getAPIAccess();

    public HotelSearchServicePortType hotelShop;
    public HotelService hotelShopService;

    @Autowired
    public HotelSearchConnector() {
        this.hotelShopService= new com.travelport.service.hotel_v48_0.HotelService();
        this.hotelShop= hotelShopService.getHotelSearchServicePort();
    }

    public void hotelSearch(HotelSearchAvailabilityReq hsaReq) {

        BaseHotelSearchRsp hsaRsp= new BaseHotelSearchRsp();

        BindingUtil.addParametersToProvider((BindingProvider)hotelShop, travelportCredentials);

        hotelSearchImpl.hotelLowFareSearch(hsaReq, hsaRsp, hotelShop);
    }
}

and based on the stack trace this the class which getting referred in line 25

package com.GDSWebService.tripnomics_GDS_integration.config.travelport;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import com.GDSWebService.tripnomics_GDS_integration.modal.TravelportCredentials;

@Configuration
@PropertySource("classpath:application.properties")
public class APIAccessConfig {

    private TravelportCredentials tpc= new TravelportCredentials();

    @Value("${GDS.travelport.endpoint}")
    private String endpoint;
    @Value("${GDS.travelport.username}")
    private String username;
    @Value("${GDS.travelport.password}")
    private String password;
    @Value("${GDS.travelport.targetbranch}")
    private String targetBranch;


    public TravelportCredentials getAPIAccess() {
//      tpc= new TravelportCredentials();

        tpc.setEndpoint(endpoint);
        tpc.setPassword(password);
        tpc.setTargetBranch(targetBranch);
        tpc.setUsername(username);

        return tpc;
    }

}

full Stack Trace:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-07-25 15:47:09.528 ERROR 7820 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hotelSearchConnector' defined in file [C:\Users\TravelSpends\Documents\Tripnomics\tripnomics-GDS-integration\target\classes\com\GDSWebService\tripnomics_GDS_integration\connector\travelport\HotelSearchConnector.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.GDSWebService.tripnomics_GDS_integration.connector.travelport.HotelSearchConnector]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:304) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:171) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1341) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1187) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:849) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at com.GDSWebService.tripnomics_GDS_integration.App.main(App.java:26) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_201]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_201]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_201]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_201]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.1.4.RELEASE.jar:2.1.4.RELEASE]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.GDSWebService.tripnomics_GDS_integration.connector.travelport.HotelSearchConnector]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:184) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:300) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    ... 24 common frames omitted
Caused by: java.lang.NullPointerException: null
    at com.GDSWebService.tripnomics_GDS_integration.connector.travelport.HotelSearchConnector.<init>(HotelSearchConnector.java:25) ~[classes/:na]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_201]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_201]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_201]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_201]
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:172) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    ... 26 common frames omitted

BaseHotelSearchRsp.java

package com.travelport.schema.hotel_v48_0;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.bind.annotation.XmlType;
import com.travelport.schema.common_v48_0.BaseSearchRsp;
import com.travelport.schema.common_v48_0.HostToken;
import com.travelport.schema.common_v48_0.MarketingInformation;


/**
 * 
 *                 Base hotel Search Response
 *             
 * 
 * <p>Java class for BaseHotelSearchRsp complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="BaseHotelSearchRsp"&gt;
 *   &lt;complexContent&gt;
 *     &lt;extension base="{http://www.travelport.com/schema/common_v48_0}BaseSearchRsp"&gt;
 *       &lt;sequence&gt;
 *         &lt;element name="ReferencePoint" type="{http://www.travelport.com/schema/hotel_v48_0}typeHotelReferencePoint" minOccurs="0"/&gt;
 *         &lt;element ref="{http://www.travelport.com/schema/hotel_v48_0}HotelSearchResult" maxOccurs="999" minOccurs="0"/&gt;
 *         &lt;element ref="{http://www.travelport.com/schema/common_v48_0}MarketingInformation" minOccurs="0"/&gt;
 *         &lt;element ref="{http://www.travelport.com/schema/common_v48_0}HostToken" minOccurs="0"/&gt;
 *       &lt;/sequence&gt;
 *       &lt;attribute name="AddressSearchQuality" type="{http://www.w3.org/2001/XMLSchema}int" /&gt;
 *     &lt;/extension&gt;
 *   &lt;/complexContent&gt;
 * &lt;/complexType&gt;
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "BaseHotelSearchRsp", propOrder = {
    "referencePoint",
    "hotelSearchResult",
    "marketingInformation",
    "hostToken"
})
@XmlSeeAlso({
    RetrieveHotelSearchAvailabilityRsp.class,
    HotelSearchAvailabilityAsynchRsp.class
})
public class BaseHotelSearchRsp
    extends BaseSearchRsp
{

    @XmlElement(name = "ReferencePoint")
    protected TypeHotelReferencePoint referencePoint;
    @XmlElement(name = "HotelSearchResult")
    protected List<HotelSearchResult> hotelSearchResult;
    @XmlElement(name = "MarketingInformation", namespace = "http://www.travelport.com/schema/common_v48_0")
    protected MarketingInformation marketingInformation;
    @XmlElement(name = "HostToken", namespace = "http://www.travelport.com/schema/common_v48_0")
    protected HostToken hostToken;
    @XmlAttribute(name = "AddressSearchQuality")
    protected Integer addressSearchQuality;

    /**
     * Gets the value of the referencePoint property.
     * 
     * @return
     *     possible object is
     *     {@link TypeHotelReferencePoint }
     *     
     */
    public TypeHotelReferencePoint getReferencePoint() {
        return referencePoint;
    }

    /**
     * Sets the value of the referencePoint property.
     * 
     * @param value
     *     allowed object is
     *     {@link TypeHotelReferencePoint }
     *     
     */
    public void setReferencePoint(TypeHotelReferencePoint value) {
        this.referencePoint = value;
    }

    /**
     * Gets the value of the hotelSearchResult property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the hotelSearchResult property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getHotelSearchResult().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link HotelSearchResult }
     * 
     * 
     */
    public List<HotelSearchResult> getHotelSearchResult() {
        if (hotelSearchResult == null) {
            hotelSearchResult = new ArrayList<HotelSearchResult>();
        }
        return this.hotelSearchResult;
    }

    /**
     * Gets the value of the marketingInformation property.
     * 
     * @return
     *     possible object is
     *     {@link MarketingInformation }
     *     
     */
    public MarketingInformation getMarketingInformation() {
        return marketingInformation;
    }

    /**
     * Sets the value of the marketingInformation property.
     * 
     * @param value
     *     allowed object is
     *     {@link MarketingInformation }
     *     
     */
    public void setMarketingInformation(MarketingInformation value) {
        this.marketingInformation = value;
    }

    /**
     * Gets the value of the hostToken property.
     * 
     * @return
     *     possible object is
     *     {@link HostToken }
     *     
     */
    public HostToken getHostToken() {
        return hostToken;
    }

    /**
     * Sets the value of the hostToken property.
     * 
     * @param value
     *     allowed object is
     *     {@link HostToken }
     *     
     */
    public void setHostToken(HostToken value) {
        this.hostToken = value;
    }

    /**
     * Gets the value of the addressSearchQuality property.
     * 
     * @return
     *     possible object is
     *     {@link Integer }
     *     
     */
    public Integer getAddressSearchQuality() {
        return addressSearchQuality;
    }

    /**
     * Sets the value of the addressSearchQuality property.
     * 
     * @param value
     *     allowed object is
     *     {@link Integer }
     *     
     */
    public void setAddressSearchQuality(Integer value) {
        this.addressSearchQuality = value;
    }

}

Kindly help guys I am a newbie to Spring Boot

Sahil Dadarya
  • 33
  • 1
  • 1
  • 4

1 Answers1

1

According to the stacktrace, it seems that the construction of your object fails because of a NullPointerException on line 25:

public TravelportCredentials travelportCredentials= apiAccessConfig.getAPIAccess();

The reason why you get this exception is that apiAccessConfig is used before it gets autowired since the field initializations are executed in a phase before Spring processes the @Autowired annotations.

To ensure that apiAccessConfig is initialized before it is used, you can write your HotelSearchConnector class as follows:

package com.GDSWebService.tripnomics_GDS_integration.connector.travelport;

import javax.xml.ws.BindingProvider;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.GDSWebService.tripnomics_GDS_integration.config.travelport.APIAccessConfig;
import com.GDSWebService.tripnomics_GDS_integration.modal.TravelportCredentials;
import com.GDSWebService.tripnomics_GDS_integration.service.HotelSearchImpl;
import com.GDSWebService.tripnomics_GDS_integration.utils.BindingUtil;
import com.travelport.schema.hotel_v48_0.BaseHotelSearchRsp;
import com.travelport.schema.hotel_v48_0.HotelSearchAvailabilityReq;
import com.travelport.service.hotel_v48_0.HotelSearchServicePortType;
import com.travelport.service.hotel_v48_0.HotelService;

@Component
public class HotelSearchConnector {

    public APIAccessConfig apiAccessConfig;

    public HotelSearchImpl hotelSearchImpl;

    public TravelportCredentials travelportCredentials;

    public HotelSearchServicePortType hotelShop;
    public HotelService hotelShopService;

    @Autowired
    public HotelSearchConnector(APIAccessConfig apiAccessConfig, HotelSearchImpl hotelSearchImpl) {
        this.apiAccessConfig = apiAccessConfig;
        this.hotelSearchImpl = hotelSearchImpl;
        this.travelportCredentials= apiAccessConfig.getAPIAccess();

        this.hotelShopService= new com.travelport.service.hotel_v48_0.HotelService();
        this.hotelShop= hotelShopService.getHotelSearchServicePort();
    }

    public void hotelSearch(HotelSearchAvailabilityReq hsaReq) {

        BaseHotelSearchRsp hsaRsp= new BaseHotelSearchRsp();

        BindingUtil.addParametersToProvider((BindingProvider)hotelShop, travelportCredentials);

        hotelSearchImpl.hotelLowFareSearch(hsaReq, hsaRsp, hotelShop);
    }
}

Notice that in my example I've replaced field injection with constructor injection.

Marko Previsic
  • 1,820
  • 16
  • 30
  • hi @marko can you please give me any hint why the field injection is not working in my code. – Sahil Dadarya Jul 26 '19 at 08:57
  • The issue was not that the field injection didn't work. The autowired fields would be set properly but the problem was that you accessed apiAccessConfig.getAPIAccess() in the declaration of the travelportCredentials field. This statement gets called before the "@Autowired" field get injected which causes a NullPointerException. This also interrupts the creation of your bean, so the field injection also doesn't happen because the error happened before the field injection. – Marko Previsic Jul 26 '19 at 09:02
  • Haha, read logs! Thanks! – Anurag Bhalekar Feb 20 '23 at 08:30