M
M
Maks Burkov2017-03-23 14:02:50
Java
Maks Burkov, 2017-03-23 14:02:50

Where does Exception in thread "main" java.lang.StackOverflowError come from?

From the stack you can see that something is happening between RegistrationService and RegistrationDataDistributor .. I don’t understand what?
Added line numbering for classes..
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("[email protected]");
        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("[email protected]");
        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());
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Oparin, 2017-03-23
@losse_narmo

private RegistrationDataDistributor distributor = new RegistrationDataDistributor();  // 18 line

There is an eternal recursion going on here.
When creating an object of the RegistrationDataDistributor class (which inherits RegistrationService), the distributor field is initialized - an object of the RegistrationDataDistributor class is created, and so on until the stack runs out

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question