Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过构造函数创建bean实例化时出错失败_Java_Spring_Spring Boot_Spring Mvc - Fatal编程技术网

Java 通过构造函数创建bean实例化时出错失败

Java 通过构造函数创建bean实例化时出错失败,java,spring,spring-boot,spring-mvc,Java,Spring,Spring Boot,Spring Mvc,通过构造函数实例化Bean失败;嵌套异常为org.springframework.beans.beanInstationException:实例化构造函数失败抛出异常;嵌套异常是java.lang.NullPointerException 在该类中引发异常: package com.GDSWebService.tripnomics_GDS_integration.connector.travelport; import javax.xml.ws.BindingProvider; import

通过构造函数实例化Bean失败;嵌套异常为org.springframework.beans.beanInstationException:实例化构造函数失败抛出异常;嵌套异常是java.lang.NullPointerException

在该类中引发异常:

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);
    }
}
基于堆栈跟踪,这个类将在第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;
    }

}
完整堆栈跟踪:

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
* * * *列表中允许以下类型的对象 *{@link HotelSearchResult} * * */ 公共列表getHotelSearchResult(){ 如果(hotelSearchResult==null){ hotelSearchResult=新建ArrayList(); } 返回此.hotelSearchResult; } /** *获取marketingInformation属性的值。 * *@返回 *可能的对象是 *{@link MarketingInformation} * */ 公共市场信息获取市场信息(){ 返回市场信息; } /** *设置marketingInformation属性的值。 * *@param值 *允许的对象是 *{@link MarketingInformation} * */ 公共无效设置MarketingInformation(MarketingInformation值){ 这个。市场信息=价值; } /** *获取hostToken属性的值。 * *@返回 *可能的对象是 *{@link HostToken} * */ 公共HostToken getHostToken(){ 返回hostToken; } /** *设置hostToken属性的值。 * *@param值 *允许的对象是 *{@link HostToken} * */ 公共无效setHostToken(HostToken值){ this.hostToken=值; } /** *获取addressSearchQuality属性的值。 * *@返回 *可能的对象是 *{@link Integer} * */ 公共整数getAddressSearchQuality(){ 退货质量; } /** *设置addressSearchQuality属性的值。 * *@param值 *允许的对象是 *{@link Integer} * */ public void setAddressSearchQuality(整数值){ 此.addressSearchQuality=值; } }
请帮助大家我是Spring Boot的新手根据stacktrace,由于第25行出现NullPointerException,对象的构造似乎失败了:

出现此异常的原因是
apiAccessConfig
在自动连接之前使用,因为字段初始化是在Spring处理@autowired注释之前的一个阶段执行的

要确保apiAccessConfig在使用前已初始化,可以按如下方式编写HotelSearchConnector类:


请注意,在我的示例中,我已替换为。

能否显示此类
BaseHotelSearchRsp
?@Deadpool添加了yes。这一行导致了问题
HotelSearchConnector.java:25
您能给我指一下行public TravelportCredentials TravelportCredentials=apiAccessConfig.getAPIAccess()吗;你好@marko你能给我一些提示吗?为什么我的代码中的字段注入不起作用。问题不是字段注入不起作用。自动连线字段将被正确设置,但问题是您在travelportCredentials字段的声明中访问了apiAccessConfig.getAPIAccess()。在注入“@Autowired”字段之前调用此语句,该字段会导致NullPointerException。这也会中断bean的创建,因此字段注入也不会发生,因为错误发生在字段注入之前。
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;
    }

}
public TravelportCredentials travelportCredentials= apiAccessConfig.getAPIAccess();
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);
    }
}