1

尝试运行我的程序时,我不断收到此错误。

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)

它仅在尝试从某个类访问方法后发生。

这是主要课程:

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);
            }
        }
    }
}

这是上面代码访问的类的代码:

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()
    {

    }


}

一旦我删除了对 exportProperties 类的调用,程序就会完美运行。带有错误消息,最后两行

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

他们在它结束之前重复了很多次。

我已经用谷歌搜索了,然后用谷歌搜索,我不知道发生了什么..感谢您的帮助!

4

1 回答 1

8

你的serverProperties班级有这个:

exportProperties writeProperties = new exportProperties();

...创建一个exportProperties. 但是exportPropertiesextends serverProperties,构造一个将再次exportProperties执行上面的行......所以它继续。

在不知道你想要达到的目标的情况下,除了“不要那样做”之外,很难提供非常具体的帮助。我怀疑你不应该在这里使用继承。(目前还不清楚你为什么要扩展JPanel......尝试更喜欢组合而不是继承。)

(您还应该修改您的类名以遵循 Java 约定...)

于 2013-01-27T09:09:39.067 回答