Brilliance Of Ben
Brilliance Of Ben

Reputation: 91

StackOverflowError No idea what is wrong

I'm continuously receiving this error when trying to run my program.

Exception in thread "main" java.lang.StackOverflowError

at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:244)
at javax.swing.UIDefaults.getUI(UIDefaults.java:752)
at javax.swing.UIManager.getUI(UIManager.java:1001)
at javax.swing.JPanel.updateUI(JPanel.java:109)
at javax.swing.JPanel.<init>(JPanel.java:69)
at javax.swing.JPanel.<init>(JPanel.java:92)
at javax.swing.JPanel.<init>(JPanel.java:100)
at serverProperties.<init>(serverProperties.java:164)
at exportProperties.<init>(exportProperties.java:8)
at serverProperties.<init>(serverProperties.java:162)

It only happens after trying to access a method from a certain class.

This is the main class:

public class serverProperties extends JPanel
{
    //Add classes
    exportProperties writeProperties = new exportProperties();

    //Assume I created the array sent to the exportProperties class

    public serverProperties()
    {
        CheckBoxListener checkListener = new CheckBoxListener();

        //Assume I created the check box and added the listener for it
    }


    private class CheckBoxListener implements ItemListener
    {

        public void itemStateChanged (ItemEvent event)
        {
            JCheckBox checkBox;
            checkBox= (JCheckBox)event.getSource();

            //Process check Boxes
            if (checkBox.isSelected())
            {
                checkBox.setText("Yes");
                writeProperties.exportFile(propertiesArr);
            }
        }
    }
}

This is the code for the class accessed by the code above:

public class exportProperties extends serverProperties
{


    public void exportProperties(String[] args)
    {
        exportFile(args);
    }

    public static void exportFile(String[] propertiesArr)
    {
        try
        {
            FileWriter outFile = new FileWriter("Path/file.txt");
            BufferedWriter out = new BufferedWriter(outFile);

            //Loop through properties
            for (int i=0; i < 27; i++)
            {
                out.append(propertiesArr[i]);
            }
            //out.write("Test");
            out.close();

        }
        catch (IOException exception)
        {
            exception.printStackTrace();
        }
    }

    public void importFile()
    {

    }


}

Once I remove the call to the exportProperties class the program work perfectly. With the error message, the last two lines

at exportProperties.<init>(exportProperties.java:8)
at serverProperties.<init>(serverProperties.java:162)

they repeat MANY times before it just ends.

I've googled, and googled, I've got no idea what is going on.. I appreciate the help!

Upvotes: 1

Views: 212

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503180

Your serverProperties class has this:

exportProperties writeProperties = new exportProperties();

... which creates an instance of exportProperties. But exportProperties extends serverProperties, to constructing an exportProperties will execute the above line again... and so it continues.

Without knowing what you're trying to achieve, it's hard to give very concrete help beyond "don't do that". I suspect you shouldn't be using inheritance here. (It's not clear why you're extending JPanel either... try to prefer composition over inheritance.)

(You should also fix your class names to follow Java conventions...)

Upvotes: 8

Related Questions