0

I have a parent class "Application Window" and a child class "config". When I am creating an object of "config" class execution tends to go into a loop and keeps on creating this object.

Following is the code snipppet:

public class ApplicationWindow implements ActionListener{
    public String workSpace;
    public String logFile;
    public JFrame frmGenericAutomationFramework;
    public JProgressBar progressBar;
    public File currentTestSuiteFolder;
    public String currentTestSuiteName;

    config cfg;
    SettingsFrame settingsFrame;
    TestSuiteFrame testSuiteFrame;
    PromptTestSuiteName testSuitePrompt;

    public ApplicationWindow (){
        initialize();

        //**cfg  = new config();**
        cfg.readProperties();
    }
}

Child class "config" below:

public class config extends ApplicationWindow{
    String str;
    File cfgfile;
    FileOutputStream out;
    FileInputStream in;
    Properties props;

    String filepath = "D:/Webdriverwork/GAF/res/elements.properties";

    public config (){
        try{
            cfgfile = new File(filepath);
            in = new FileInputStream(cfgfile);
            props = new Properties();       
        }

        catch (Exception e){
            // Log message in log file
            String message = e.getMessage();

            System.out.println(message);

            // Exit the system
            System.exit(0);
        }
    }

    public void readProperties (){
        try{
            props.load(in);

            workSpace = props.getProperty("WORKSPACE");
            logFile = props.getProperty("LOGFILE");
        }
        catch (Exception e){
            // Log message in log file
            String message = e.getMessage();

            System.out.println(message);

            // Exit the system
            System.exit(0);
        }
    }

    public void updateProperty (String key, String value){
        try{
            props.setProperty(key,value);
        }
        catch (Exception e){
            // Log message in log file
            String message = e.getMessage();

            System.out.println(message);

            // Exit the system
            System.exit(0);
        }       
    }

    public void writeProperties (){
        try{
            in.close();
            out = new FileOutputStream(cfgfile);
            props.store(out, null);
            out.close();
        }
        catch (Exception e){
            // Log message in log file
            String message = e.getMessage();

            System.out.println(message);

            // Exit the system
            System.exit(0);
        }
    }
}
  • You seem to have received good answers already, but remember that you can add a breakpoint to the code and debug your application in Eclipse. That will give you the chance to exactly how the code flows and see what went wrong. – Fredrik Jan 23 '14 at 09:18
  • Thanks for the advice. But I have always remembered that. :) – deejaydrives Jan 23 '14 at 11:34

6 Answers6

0

This is the problem:

public class config extends ApplicationWindow

in conjunction with this in the ApplicationWindow constructor:

cfg  = new config();

So to create a new ApplicationWindow, you need to create a new config... but that's an ApplicationWindow itself, so it'll create another config... which will in turn create another config etc.

Why would config extend ApplicationWindow? That sounds like an odd design to me - a configuration isn't a window. Just get rid of that extends specification and you might find everything else works.

Additionally, I would strongly advise you to follow Java naming conventions (use PascalCase for class names, for example) and make all of your fields private.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

Config object is being created in cyclic manner. when you create Config object then implicit no argument constructor of ApplicationWindow class executes and one more time it creates an object of config Object and it repeats.enter image description here

Remember one thing,
implicit no-argument constructor of super class will always be called even if you don't call it.

public Config()
{
    super();   // compiler adds this statement inside constructor even if you don't declare
}

and your superclass ApplicationWindow is creating yet another Config object.

To avoid this, remove new Config() object creation line from ApplicationWindow class constructor, which will stop looping. and you already have the reference of Config object created.

AmitG
  • 10,365
  • 5
  • 31
  • 52
0

You have created Child class object in Constructor of Parent class. When you created Object of Child Class it's constructor gets called, which basically calls Parent Class Constructor first then executes Child Class Constructor, where again child class object getting created and it's getting repeated again and again. So it's causing recursive operation here, hence infinite loop.

Rameez
  • 1,712
  • 2
  • 18
  • 39
0

config must not extend ApplicationWindow, since you have the following situation: AppWindow calls the constructor from config, and config constructor calls its super constructor which is AppWindow -> endless

Manuel Manhart
  • 4,819
  • 3
  • 24
  • 28
0

Why do you make config extends from ApplicationWindow? You should remove this inheritance relationship.

Your code will cause Stack Overflow Error as subclass's constructor will first call its parent constructor at initialization. The problem is your ApplicationWindow, as parent class of config, calls constructor of its subclass, as is unexpected and will cause infinite loop.

Li Zhanhui
  • 61
  • 4
0

If your intention is to call readProperties() from ApplicationWindow, then make it an abstract method and remove the cfg reference.

Ted Bigham
  • 4,237
  • 1
  • 26
  • 31