Maks00088
@Maks00088
Обучаюсь Java/JavaEE /Php/Js

Откуда появляется Exception in thread «main» java.lang.StackOverflowError?

Из стека видно что чтото происходит между RegistrationService и RegistrationDataDistributor .. Не пойму что ?
Добавил нумерацию линий для класов ..

a0da0a1b9d1f4e9db7273c3349711cde.jpg
public class RegistrationService extends JdbcService{  // 15 line

    private AdminService adminService = getAdminServiceInstance();
    private RegistrationDataDistributor distributor = new RegistrationDataDistributor();  // 18 line
    private CompanyService companyService = getCompanyServiceInstance();

    public void RegistrationDataHandler(RegistrationData registrationData) throws ConnectionFailed, CouponSystemException, CompanyNotExist {
       distributor.distributeTheData(registrationData);
    }


    protected void queryDatabase(Map<String,Object> mappedRegData) throws ConnectionFailed, CouponSystemException, CompanyNotExist {
        if(mappedRegData.containsKey("user") && mappedRegData.containsKey("company")){
                adminService.createCompany((Company)mappedRegData.get("company"));
                insertUser((User) mappedRegData.get("user"));
        }if(mappedRegData.containsKey("user") && mappedRegData.containsKey("customer")){
                companyService.createCustomer((Customer)mappedRegData.get("customer"));
                insertUser((User) mappedRegData.get("user"));
        }if(mappedRegData.containsKey("user") && mappedRegData.containsKey("employee")){
                Employee employee = (Employee)mappedRegData.get("employee");
                companyService.createCompanyEmployee(employee.getCompany(),employee);
                insertUser((User) mappedRegData.get("user"));
        }
    }


public class RegistrationDataDistributor extends RegistrationService{
//14 line
    private Map<String,Object> mappedRegData = new HashMap<>();

    private void clearMappedRegData(){
        mappedRegData.clear();
    }

    
    public void distributeTheData(RegistrationData data) throws ConnectionFailed, CouponSystemException, CompanyNotExist {
        if(data.getAccountType().equals(AccountType.Company)){
            mappedRegData.put("company",data.getCompany());
            mappedRegData.put("user",data.getUser());
            queryDatabase(mappedRegData);
                clearMappedRegData();
        }else if(data.getAccountType().equals(AccountType.Customer)){
                mappedRegData.put("customer",data.getCustomer());
                mappedRegData.put("user",data.getUser());
                queryDatabase(mappedRegData);
                clearMappedRegData();
        }else if(data.getAccountType().equals(AccountType.Employee)){
            mappedRegData.put("employee",data.getEmployee());
            mappedRegData.put("user",data.getUser());
            queryDatabase(mappedRegData);
                clearMappedRegData();
            }
        }
}


Main:
====
public static void main(String[] args) {

        CouponSystemBeanFactory couponBeanFactory = CouponSystemBeanFactory.getCouponBeanFactoryInstance();
        RegistrationService registrationService = couponBeanFactory.getRegistrationServiceInstance();

        RegistrationData registration = couponBeanFactory.getBeanInstance("registration",RegistrationData.class);

        Company company = couponBeanFactory.getBeanInstance("company",Company.class);
        company.setId(547643535);

        Employee employee = couponBeanFactory.getBeanInstance("employee",Employee.class);
        employee.setId(4532432);
        employee.setEmail("Maks.Burkov88@gmail.com");
        employee.setCompany(company);
        employee.setCountry("Israel");
        employee.setFirstName("Maks");
        employee.setLastName("Burkov");
        employee.setPhoneNumber("0545808485");

        User user = new User();
        user.setNickName("Maks19880");
        user.setPassword("8767865");
        user.setEmail("Maks.Burkov88@gmail.com");
        user.setRole("customer");

        registration.setAccountType(AccountType.Employee);
        registration.setEmployee(employee);
        registration.setUser(user);

        try {
            registrationService.RegistrationDataHandler(registration);
        } catch (ConnectionFailed connectionFailed) {
            System.out.println(connectionFailed.getMessage());
        } catch (CouponSystemException e) {
            System.out.println(e.getMessage());
        } catch (CompanyNotExist companyNotExist) {
            System.out.println(companyNotExist.getMessage());
        }
    }
  • Вопрос задан
  • 482 просмотра
Пригласить эксперта
Ответы на вопрос 1
private RegistrationDataDistributor distributor = new RegistrationDataDistributor();  // 18 line


Тут происходит вечная рекурсия.
При создании объекта класса RegistrationDataDistributor (который наследует RegistrationService) происходит инициализация поля distributor - создается объект класса RegistrationDataDistributor и так пока не кончится стек
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы