close

[Solved] Error creating bean Bean instantiation via constructor failed

Hello Guys, How are you all? Hope You all Are Fine. Today I am facing the following error Error creating bean Bean instantiation via constructor failed in Java. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Error creating bean Bean instantiation via constructor failed Error Occurs?

Today I am facing the following error Error creating bean Bean instantiation via constructor failed in Java.

How To Solve Error creating bean Bean instantiation via constructor failed Error ?

  1. How To Solve Error creating bean Bean instantiation via constructor failed Error ?

    To Solve Error creating bean Bean instantiation via constructor failed Error According to the stacktrace, it seems that the construction of your object fails because of a NullPointerException on line 25:

  2. Error creating bean Bean instantiation via constructor failed

    To Solve Error creating bean Bean instantiation via constructor failed Error According to the stacktrace, it seems that the construction of your object fails because of a NullPointerException on line 25:

Solution 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.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read