0

I need your help to understand why in my method the object allocation returns Stack Overflow error.

Here is the method:

public String toJson(){

        JSONObject json;
        json = new JSONObject(this); //error happens here...

        return json.toString();
    }

Here all code from this class:

package com.neocloud.model;

import java.io.Serializable;

import com.amazonaws.util.json.JSONObject;
import com.neocloud.amazon.AwsSns;
import com.neocloud.model.dao.DeviceDAO;
import com.neocloud.model.dao.GroupDAO;
import com.neocloud.model.dao.SubscriptionDAO;

public class Subscription implements Serializable{

    private static final long serialVersionUID = -3901714994276495605L;
    private long id;
    private Device dispositivo;
    private Group  grupo;
    private String subscription;


    public Subscription(){

        clear();

    }

    public void clear(){

        id = 0;
        subscription = "";
        grupo = new Group();
        dispositivo = new Device();

    }

    public String toJson(){

        JSONObject json;
        json = new JSONObject(this); //error happens here...

        return json.toString();
    }


    public String registerDB(boolean registrarSNS){

        GroupDAO gDAO = new GroupDAO();
        DeviceDAO dDAO = new DeviceDAO();

        gDAO.selectById(this.grupo);
        dDAO.selectById(this.dispositivo);

        if (registrarSNS)
            registerSNS(this.dispositivo.getEndPointArn(), this.grupo.getTopicArn());

        SubscriptionDAO sDAO = new SubscriptionDAO();
        return sDAO.insert(this);
    }

    public String registerSNS(String endpointArn, String topicArn){

        String resposta;
        AwsSns sns = AwsSns.getInstance();
        resposta = ""+sns.addSubscriptionToTopic(endpointArn, topicArn);
        setSubscription(resposta);

        return resposta;        
    }

    public String delete(){

        AwsSns sns = AwsSns.getInstance();
        SubscriptionDAO sDAO = new SubscriptionDAO();
        sDAO.selectById(this);

        sns.deleteSubscriptionFromTopic(this.subscription);

        return sDAO.delete(this);

    }


    public long getId() {
        return id;
    }

    public void setId(long id) {
        if (this.id != id)
            this.id = id;
    }

    public String getSubscription() {
        return subscription;
    }

    public void setSubscription(String subscription) {
        if (subscription != null){
            if (!this.subscription.equals(subscription))
                this.subscription = subscription;
        }else
            this.subscription = new String();
    }

    public Device getDispositivo() {
        return dispositivo;
    }

    public void setDispositivo(Device dispositivo) {
        if (dispositivo != null){
            if (dispositivo.getId() != this.dispositivo.getId())
                this.dispositivo = dispositivo;
        }else
            this.dispositivo = new Device();
    }

    public Group getGrupo() {
        return grupo;
    }

    public void setGrupo(Group grupo) {
        if (grupo != null){
            if (grupo.getId() != this.grupo.getId())
                this.grupo = grupo;
        }else
            this.grupo = new Group();
    }

}

Here is the Device class:

package com.neocloud.model;

import java.io.Serializable;
import java.util.ArrayList;

import com.amazonaws.util.json.JSONException;
import com.amazonaws.util.json.JSONObject;
import com.neocloud.amazon.AwsSns;
import com.neocloud.model.dao.DeviceDAO;
import com.neocloud.model.enums.EEnvironment;
import com.neocloud.model.enums.EProduct;
import com.neocloud.model.enums.ESystem;

public class Device implements Serializable{


    private static final long serialVersionUID = 8515474788917476721L;
    private long id;
    private String development;
    private String SSL = "tls://gateway.push.apple.com:2195";
    private String feedback = "tls://feedback.push.apple.com:2196";
    private String sandboxSSL = "tls://gateway.sandbox.push.apple.com:2195";
    private String sandboxFeedback = "tls://feedback.sandbox.push.apple.com:2196";
    private String message;
    private String endPointArn;
    private String deviceToken;
    private EProduct appID;
    private String appVersion;
    private String deviceUID;
    private String deviceName;
    private String deviceModel;
    private String deviceVersion;
    private boolean pushBadge;
    private boolean pushAlert;
    private boolean pushSound;
    private ESystem system;
    private EEnvironment environment;
    private ArrayList<Subscription> subscriptions;
    private String deviceUser;


    public Device(){

        clear();

    }

    public String deleteToken(){
        /*
         * 
         * terminar aqui ainda...
         * 
        DeviceDAO dDAO = new DeviceDAO();

        AwsSns sns = AwsSns.getInstance();

        sns.deleteDeviceEndpoint(getEndPointArn());

        ArrayList<String> subscriptions = dDAO.getSubscriptionsFromEndpointArn(this);

        for (int i=0; i<subscriptions.size();i++){

            sns.deleteSubscriptionFromTopic(subscriptions.get(i));

        }

        return dDAO.delete(this);
        */ return "";
    }

    public String updateToken(){

        if ((system == ESystem.IOS) && (deviceToken.length() != 64))
            return "deviceTokenlen64";

        AwsSns sns = AwsSns.getInstance();
        String jsonResposta = ""+sns.updateDeviceEndpoint(getEndPointArn(), deviceToken);

        DeviceDAO dDAO = new DeviceDAO();
        dDAO.update(this);

        return jsonResposta;

    }

    public String registerDevice(){

        if (appVersion.length() == 0)
            return "applen0";
        if (deviceUID.length() > 40)
            return "deviceUID40";
        if ((system == ESystem.IOS) && (deviceToken.length() != 64))
            return "deviceTokenlen64";
        if (deviceName.length() == 0)
            return "deviceNamelen0";
        if (deviceModel.length() == 0)
            return "deviceModellen0";
        if (deviceVersion.length() == 0)
            return "deviceVersionlen0";

        AwsSns sns = AwsSns.getInstance();
        String jsonResposta = null;

        switch (getAppID()) {
            case MODULE: {
                if (system == ESystem.IOS)
                    jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTMODULEIOS);
                else if (system == ESystem.ANDROID)
                    jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTMODULEANDROID);
                break;

            }
            case HOSTPRO: {
                if (system == ESystem.IOS)
                    jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTHOSTPROIOS);
                else if (system == ESystem.ANDROID)
                    jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTHOSTPROANDROID);                  
                break;

            }
            case CONNEXOON: {
                if (system == ESystem.IOS)
                    jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTCONNEXOONIOS);
                else if (system == ESystem.ANDROID)
                    jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTCONNEXOONANDROID);
                break;

            }
            case TAHOMA: {

                break;
            }
            case MINIBOX: {

                break;
            }
            case NONE: {

            }
            default: {

            }
        }


        if (jsonResposta != null){

            JSONObject json;
            try {
                jsonResposta = jsonResposta.replace("}", "\"}").replace("arn", "\"arn");
                jsonResposta = jsonResposta.replace("{EndPointArn:", "{\"EndPointArn\":");
                json = new JSONObject(jsonResposta);
                setEndPointArn(json.getString("EndpointArn"));
            } catch (JSONException e) {

                e.printStackTrace();
            }

        }

        Subscription subscription;

        switch (getAppID()){
            case MODULE: {

                subscription = new Subscription();
                subscription.getGrupo().setId(19);
                subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICMODULEGERAL);
                subscriptions.add(subscription);


                if (system == ESystem.IOS){

                    subscription = new Subscription();
                    subscription.getGrupo().setId(25);
                    subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICMODULEIOS);
                    subscriptions.add(subscription);

                }
                else if (system == ESystem.ANDROID){

                    subscription = new Subscription();
                    subscription.getGrupo().setId(22);
                    subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICMODULEANDROID);
                    subscriptions.add(subscription);

                }
                break;

            }
            case HOSTPRO: {

                subscription = new Subscription();
                subscription.getGrupo().setId(10);
                subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICHOSTPROGERAL);
                subscriptions.add(subscription);

                if (system == ESystem.IOS){

                    subscription = new Subscription();
                    subscription.getGrupo().setId(16);
                    subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICHOSTPROIOS);
                    subscriptions.add(subscription);

                }
                else if (system == ESystem.ANDROID){

                    subscription = new Subscription();
                    subscription.getGrupo().setId(13);
                    subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICHOSTPROANDROID);
                    subscriptions.add(subscription);

                }   
                break;

            }
            case CONNEXOON: {

                subscription = new Subscription();
                subscription.getGrupo().setId(1);
                subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICCONNEXOONGERAL);
                subscriptions.add(subscription);

                if (system == ESystem.IOS){

                    subscription = new Subscription();
                    subscription.getGrupo().setId(7);
                    subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICCONNEXOONIOS);
                    subscriptions.add(subscription);

                }
                else if (system == ESystem.ANDROID){

                    subscription = new Subscription();
                    subscription.getGrupo().setId(4);
                    subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICCONNEXOONANDROID);
                    subscriptions.add(subscription);

                }
                break;

            }
            case TAHOMA: {

                break;
            }
            case MINIBOX: {

                break;
            }
            case NONE: {

            }
            default: {

            }
        }

        DeviceDAO dDAO = new DeviceDAO();
        return dDAO.insert(this);

    }


    public void clear(){

        development = "";
        message = "";
        endPointArn = "";
        deviceToken = "";
        appID = EProduct.NONE;
        appVersion = "";
        deviceUID = "";
        deviceName = "";
        deviceModel = "";
        deviceVersion = "";
        pushBadge = false;
        pushAlert = false;
        pushSound = false;
        system = ESystem.NONE;
        environment = EEnvironment.HOMOLOGATION;
        subscriptions = new ArrayList<Subscription>();
        deviceUser = "";

    }

    public String toJson(){

        JSONObject json;
        json = new JSONObject(this);

        return json.toString();

    }


    public String getDevelopment() {
        return development;
    }

    public void setDevelopment(String development) {
        if (development != null){
            if (!development.equals(this.development))
                this.development = development;
        }
        this.development = new String();
    }

    public String getSSL() {
        return SSL;
    }

    public void setSSL(String sSL) {
        if (sSL != null){
            if (!sSL.equals(this.SSL))
                this.SSL = sSL;
        }
        else
            this.SSL = new String();
    }

    public String getFeedback() {
        return feedback;
    }

    public void setFeedback(String feedback) {
        if (feedback != null){
            if (!feedback.equals(this.feedback))
                this.feedback = feedback;
        }
        this.feedback = new String();
    }

    public String getSandboxSSL() {
        return sandboxSSL;
    }

    public void setSandboxSSL(String sandboxSSL) {

        if (sandboxSSL != null){
            if (!sandboxSSL.equals(this.sandboxSSL))
                this.sandboxSSL = sandboxSSL;
        }else       
            this.sandboxSSL = new String();
    }

    public String getSandboxFeedback() {
        return sandboxFeedback;
    }

    public void setSandboxFeedback(String sandboxFeedback) {
        if (sandboxFeedback != null){
            if (!sandboxFeedback.equals(this.sandboxFeedback))
                this.sandboxFeedback = sandboxFeedback;
        }else
            this.sandboxFeedback = new String();
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        if (message != null){
            if (!message.equals(this.message))
                this.message = message;
        }else
            this.message = new String();
    }

    public String getEndPointArn() {
        return endPointArn;
    }

    public void setEndPointArn(String createPlatformEndpointResult) {
        if (createPlatformEndpointResult != null){
            if (!createPlatformEndpointResult.equals(this.endPointArn))
                this.endPointArn = createPlatformEndpointResult;
        }else
            this.endPointArn = new String();
    }

    public String getDeviceToken() {
        return deviceToken;
    }

    public void setDeviceToken(String deviceToken) {
        if (deviceToken != null){
            if (!deviceToken.equals(this.deviceToken))
                this.deviceToken = deviceToken;
        }else
            this.deviceToken = new String();
    }

    public EProduct getAppID() {
        return appID ;
    }

    public void setAppID(EProduct appID){
        if (appID != this.appID)
            this.appID = appID;
    }

    public String getAppVersion() {
        return appVersion;
    }

    public void setAppVersion(String appVersion) {
        if (appVersion != null){
            if (!appVersion.equals(this.appVersion))
                this.appVersion = appVersion;
        }else
            this.appVersion = new String();
    }

    public String getDeviceUID() {
        return deviceUID;
    }

    public void setDeviceUID(String deviceUID) {
        if (deviceUID != null){
            if (!deviceUID.equals(this.deviceUID))
                this.deviceUID = deviceUID;
        }else
            this.deviceUID = new String();
    }

    public String getDeviceName() {
        return deviceName;
    }

    public void setDeviceName(String deviceName) {
        if (deviceName != null){
            if (!deviceName.equals(this.deviceName))
                this.deviceName = deviceName;
        }else
            this.deviceName = new String();
    }

    public String getDeviceModel() {
        return deviceModel;
    }

    public void setDeviceModel(String deviceModel) {
        if (deviceModel != null){
            if (!deviceModel.equals(this.deviceModel))
                this.deviceModel = deviceModel;
        }else
            this.deviceModel = new String();
    }

    public String getDeviceVersion() {
        return deviceVersion;
    }

    public void setDeviceVersion(String deviceVersion) {
        if (deviceVersion != null){
            if (!deviceVersion.equals(this.deviceVersion))
                this.deviceVersion = deviceVersion;
        }else
            this.deviceVersion = new String();
    }

    public boolean isPushBadge() {
        return pushBadge;
    }

    public void setPushBadge(boolean pushBadge) {
        if (pushBadge != this.pushBadge)
            this.pushBadge = pushBadge;
    }

    public boolean isPushAlert() {
        return pushAlert;
    }

    public void setPushAlert(boolean pushAlert) {
        if (pushAlert != this.pushAlert)
            this.pushAlert = pushAlert;
    }

    public boolean isPushSound() {
        return pushSound;
    }

    public void setPushSound(boolean pushSound) {
        if (this.pushSound != pushSound)
            this.pushSound = pushSound;
    }

    public ESystem getSystem() {
        return system;
    }

    public void setTipoSmartphone(ESystem system) {
        if (system != this.system)
            this.system = system;
    }

    public EEnvironment getEnvironment() {
        return environment;
    }

    public void setEnvironment(EEnvironment environment) {
        if (environment != this.environment)
            this.environment = environment;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        if (this.id != id)
            this.id = id;
    }

    public void setSystem(ESystem system) {
        if (system != this.system)
    this.system = system;
    }

    public ArrayList<Subscription> getSubscriptions() {
        return subscriptions;
    }

    public void setSubscriptions(ArrayList<Subscription> subscriptions) {
        if (subscriptions != null)
            this.subscriptions = subscriptions;
        else
            this.subscriptions = new ArrayList<Subscription>();
    }

    public String getDeviceUser() {
        return deviceUser;
    }

    public void setDeviceUser(String deviceUser) {
        if (deviceUser != null){
            if (!deviceUser.equals(this.deviceUser))
                this.deviceUser = deviceUser;
        }else
            this.deviceUser = new String();
    }



}

Here is the Group class:

package com.neocloud.model;

import java.io.Serializable;
import java.util.ArrayList;

import com.amazonaws.util.json.JSONException;
import com.amazonaws.util.json.JSONObject;
import com.neocloud.amazon.AwsSns;
import com.neocloud.model.dao.GroupDAO;

public class Group implements Serializable{


    private static final long serialVersionUID = -5032857327241801763L;
    private long id;
    private String nome;
    private String topicArn;

    public Group(){
        this.clear();
    }

    public void clear(){
        this.id = 0;
        this.nome = new String();
        this.topicArn = new String();
    }

    public String registerGrupo(){

        AwsSns snsClient = AwsSns.getInstance();

        String topic = ""+snsClient.createTopic(this.nome);

        JSONObject json;
        String jsonResposta = topic;
        try {
            jsonResposta = jsonResposta.replace("}", "\"}").replace("arn", "\"arn");
            jsonResposta = jsonResposta.replace("{TopicArn:", "{\"TopicArn\":");
            json = new JSONObject(jsonResposta);
            setTopicArn(json.getString("TopicArn"));
        } catch (JSONException e) {

            e.printStackTrace();
        }

        GroupDAO grupoDAO = new GroupDAO();
        return grupoDAO.insert(this);

    }

    public String deleteGrupo(){
        AwsSns snsClient = AwsSns.getInstance();

        GroupDAO grupoDAO = new GroupDAO();
        ArrayList<Group> grupos = grupoDAO.select(this);

        if (grupos.size() > 0){

            for (int i = 0; i<grupos.size(); i++){
                this.topicArn = grupos.get(i).getTopicArn();
                this.nome = grupos.get(i).getNome();
                this.id = grupos.get(i).getId();
                snsClient.deleteTopic(this.topicArn);

            }

            return grupoDAO.delete(this);

        }

        return "-1";
    }

    public String toJson(){

        JSONObject json;
        json = new JSONObject(this);

        return json.toString();

    }

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public String getTopicArn() {
        return topicArn;
    }
    public void setTopicArn(String topicArn) {
        this.topicArn = topicArn;
    }

}

Here is the Tomcat log:

Exception in thread "http-bio-8080-exec-5" java.lang.StackOverflowError at java.lang.reflect.Executable.(Unknown Source) at java.lang.reflect.Method.(Unknown Source) at java.lang.reflect.Method.copy(Unknown Source) at java.lang.reflect.ReflectAccess.copyMethod(Unknown Source) at sun.reflect.ReflectionFactory.copyMethod(Unknown Source) at java.lang.Class.copyMethods(Unknown Source) at java.lang.Class.getMethods(Unknown Source) at com.amazonaws.util.json.JSONObject.populateMap(JSONObject.java:930) at com.amazonaws.util.json.JSONObject.(JSONObject.java:285) at com.amazonaws.util.json.JSONObject.wrap(JSONObject.java:1540) at com.amazonaws.util.json.JSONObject.populateMap(JSONObject.java:960)

Thanks for all help!

2
  • 2
    have you tried debugging your application? Commented Feb 26, 2016 at 14:08
  • Can you show more of the stack trace do you can see the section which is repeating? Commented Feb 26, 2016 at 14:11

3 Answers 3

2

Maybe a circular reference in your object.

Sign up to request clarification or add additional context in comments.

Comments

1
json = new JSONObject(this);

You're trying to JSONify this

id = 0;
subscription = "";
grupo = new Group();
dispositivo = new Device();

And i think that somehow, it cant JSONify Group and Device. Could you show us the code for those classes ?

UPDATE : In your Subscription.clear() method, you affect a new Group() & a new Device(). When creating a new Device(), the method Device.clear() get called and create a new ArrayList<Subscription>(), that'll call the method Subscription.clear(), that'll affect a new Group() & a new Device() and so on. You're infinitely looping, that's why you get the StackOverflow error. It's weird that you're subscription is made of a Group that is made of an ArrayList of subscription who are also mades of Groups etc.

3 Comments

@pedro.olimpio updated, check if you could work around your model
Thanks for help @Jorel I'm changing the circular reference here... Thanks a lot.
@pedro.olimpio glad i've been able to help you out. Cheers buddy !
1

Maybe you have a circular reference...

Object a = X;
Object c = Y;

a.prop = Y;
c.prop2 = X;

then, your JSON(a) is

{
    prop : {
      prop2 : { 
       prop : Y (Y have prop2 again)
       /*infinite..*/
      }
    }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.