{"id":1546,"date":"2022-08-30T15:17:26","date_gmt":"2022-08-30T15:17:26","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/27\/stack-overflow-error-with-gui-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:17:26","modified_gmt":"2022-08-30T15:17:26","slug":"stack-overflow-error-with-gui-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/stack-overflow-error-with-gui-collection-of-common-programming-errors\/","title":{"rendered":"Stack overflow error with GUI-Collection of common programming errors"},"content":{"rendered":"<p>Alright so I am making a calculater, and I am getting a stack overflow error, i&#8217;m guessing because it&#8217;s trying to handle to much data.<\/p>\n<pre><code>import java.awt.*;\nimport javax.swing.*;\nimport java.awt.Dimension;\nimport java.awt.Graphics;\nimport java.awt.event.ActionEvent;\nimport java.awt.event.ActionListener;\n\npublic class Size extends JPanel implements ActionListener {\n\ndouble base,size;\nint shoesize;\nString race;\n\nJButton calc = new JButton(\"Calculate\");\n\nJTextField textsize = new JTextField(20);\n\npublic Size() {\n    \/\/JButton calc;\n    System.out.println(\"Started the adding\");\n\n    calc.addActionListener(this);\n    textsize.addActionListener(this);\n\n    calc.setBounds(135, 200, 120, 40);\n    textsize.setBounds(15,40,70,20);\n\n    add(calc);\n    add(textsize);\n\n    setPreferredSize(new Dimension(400, 300));\n    setLayout(null);\n}\n\npublic static void main(String[] args) {\n    JFrame frame = new JFrame(\"Size calc\");\n    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\n    frame.getContentPane().add(new Size());\n    frame.pack();\n    frame.setVisible(true);\n}\n\n@Override\npublic void paint(Graphics g){\n    DrawStats(g);\n}\n\npublic void DrawStats(Graphics g) {\n    g.setFont(new Font(null, Font.PLAIN, 12));\n    g.setColor(Color.red);\n    g.drawString(\"Aprrox Size: \" + size, 135, 15);\n    paint(g);\n}\n\npublic void actionPerformed(ActionEvent e) {\n    if (e.getSource() == calc) {\n        try {\n            String ShoeSize = textsize.getText();\n\n            shoesize = Integer.parseInt(ShoeSize);  \n            size = shoesize\/2;\n        } catch (Exception j) {\n            System.out.println(\"Nothing inside of the text field\");\n        } \n    }\n    }\n}\n<\/code><\/pre>\n<p>when I comment out paint(g) I no longer get the error:<\/p>\n<pre><code> Exception in thread \"AWT-EventQueue-0\" java.lang.StackOverflowError\nat java.lang.Character.toLowerCase(Unknown Source)\nat java.lang.Character.toLowerCase(Unknown Source)\nat java.lang.String.toLowerCase(Unknown Source)\nat sun.font.SunFontManager.findFont2D(Unknown Source)\nat java.awt.Font.getFont2D(Unknown Source)\nat java.awt.Font.access$000(Unknown Source)\nat java.awt.Font$FontAccessImpl.getFont2D(Unknown Source)\nat sun.font.FontUtilities.getFont2D(Unknown Source)\nat sun.java2d.SunGraphics2D.checkFontInfo(Unknown Source)\nat sun.java2d.SunGraphics2D.getFontInfo(Unknown Source)\nat sun.java2d.pipe.GlyphListPipe.drawString(Unknown Source)\nat sun.java2d.SunGraphics2D.drawString(Unknown Source)\n<\/code><\/pre>\n<p>I want it to update &#8220;Aproox size&#8221; in real time<\/p>\n<ol>\n<li>\n<p>There is a cyclic dependency between <code>paint<\/code> and <code>DrawStats<\/code> \u2014 each one calls the other. Don&#8217;t call paint directly. Rather invoke <code>repaint<\/code>. Also override <code>paintComponent<\/code> rather than <code>paint<\/code> and invoke <code>super.paintComponent(g)<\/code>.<\/p>\n<p>Remove the methods <code>paint<\/code> and <code>DrawStats<\/code> and replace with this<\/p>\n<pre><code>@Override\npublic void paintComponent(Graphics g) {\n\n   super.paintComponent(g));\n   g.setFont(new Font(\"SansSerif\", Font.PLAIN, 12));\n   g.setColor(Color.red);\n   g.drawString(\"Aprrox Size: \" + size, 135, 15);\n}\n<\/code><\/pre>\n<p>Use a Swing Timer to invoke <code>repaint<\/code> if periodic repaints are required.<\/p>\n<p>Aside: Use Java naming conventions when naming method names such as <code>drawStats<\/code>.<\/p>\n<\/li>\n<li>\n<p>Ouch&#8230; Infinite recursion<\/p>\n<pre><code>@Override\npublic void paint(Graphics g){\n    DrawStats(g); \/\/ infinite recursion\n}\npublic void DrawStats(Graphics g) {\n    g.setFont(new Font(null, Font.PLAIN, 12));\n    g.setColor(Color.red);\n    g.drawString(\"Aprrox Size: \" + size, 135, 15);\n    paint(g); \/\/ infinite recursion\n}\n<\/code><\/pre>\n<p>I suppose you see this in your stack trace:<\/p>\n<pre><code>...\nat Size.paint\nat Size.DrawStats\nat Size.paint\nat Size.DrawStats\nat Size.paint\nat Size.DrawStats\n(a lot more)...\n<\/code><\/pre>\n<p>Remove <code>paint(g);<\/code> in <code>DrawStats<\/code><\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-27 05:08:33. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Alright so I am making a calculater, and I am getting a stack overflow error, i&#8217;m guessing because it&#8217;s trying to handle to much data. import java.awt.*; import javax.swing.*; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Size extends JPanel implements ActionListener { double base,size; int shoesize; String race; JButton calc = new [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1546","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1546"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1546\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}