Results 1 to 4 of 4
  1. #1
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default Simple linking of class1 to class2 Problem help me please

    I want to link my class1 to class2 but I have an error
    java.lang.StackOverflowError
    at sun.awt.Win32GraphicsConfig.getBounds(Native Method)
    at sun.awt.Win32GraphicsConfig.getBounds(Unknown Source)
    at java.awt.Window.init(Unknown Source)
    at java.awt.Window.<init>(Unknown Source)
    at java.awt.Frame.<init>(Unknown Source)
    at java.awt.Frame.<init>(Unknown Source)
    at javax.swing.JFrame.<init>(Unknown Source)
    This is supposed to be a simple chat program, when I hit the send button in class1, the class2 will appear.

    Here's the code:
    Class1.java
    Java Code:
    public class class1 extends JFrame {
    	class2 c2 = new class2();
    	
        public class1() {
            lblHeader = new JLabel();
            jScrollPane1 = new JScrollPane();
            txtAreaChat = new JTextArea();
            txtEnterChat = new JTextField();
            btnSend = new JButton();
    
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            getContentPane().setLayout(null);
    
            lblHeader.setText("Chatter 1");
    
            txtAreaChat.setColumns(20);
            txtAreaChat.setEditable(false);
            txtAreaChat.setRows(5);
            
            jScrollPane1.setViewportView(txtAreaChat);
            
            btnSend.setText("Send");
            btnSend.addActionListener(
            		new ActionListener(){
            			public void actionPerformed(ActionEvent e){
            				c2.setVisible(true);
            			}
            		}
            );
            
            getContentPane().add(lblHeader);
            getContentPane().add(jScrollPane1);
            getContentPane().add(txtEnterChat);
            getContentPane().add(btnSend);
    
            lblHeader.setBounds(70, 10, 50, 14);
            txtEnterChat.setBounds(10, 220, 160, 20);
            jScrollPane1.setBounds(10, 40, 166, 170);
            btnSend.setBounds(60, 250, 75, 23);      
            
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            setBounds((screenSize.width-196)/2, (screenSize.height-312)/2, 196, 312);
        }
    class2.java
    Java Code:
    public class class2 extends JFrame {
    	class1 c1 = new class1();
    	
        public class2() {
            lblHeader = new JLabel();
            jScrollPane1 = new JScrollPane();
            txtAreaChat = new JTextArea();
            txtEnterChat = new JTextField();
            btnSend = new JButton();
    
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            getContentPane().setLayout(null);
    
            lblHeader.setText("Chatter 1");
    
            txtAreaChat.setColumns(20);
            txtAreaChat.setEditable(false);
            txtAreaChat.setRows(5);
            
            jScrollPane1.setViewportView(txtAreaChat);
            
            btnSend.setText("Send");
            btnSend.addActionListener(
            		new ActionListener(){
            			public void actionPerformed(ActionEvent e){
            				c1.setVisible(true);
            			}
            		}
            );
            
            getContentPane().add(lblHeader);
            getContentPane().add(jScrollPane1);
            getContentPane().add(txtEnterChat);
            getContentPane().add(btnSend);
    
            lblHeader.setBounds(70, 10, 50, 14);
            txtEnterChat.setBounds(10, 220, 160, 20);
            jScrollPane1.setBounds(10, 40, 166, 170);
            btnSend.setBounds(60, 250, 75, 23);
    
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            setBounds((screenSize.width-196)/2, (screenSize.height-312)/2, 196, 312);
        }
    Please help me guys..

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You've got infinite recursion going on. If you create a class1 object, it creates a class2 object which creates a class1 object which creates a class2 object, which creates a class1 object which creates a class2 object, which creates a class1 object which creates a class2 object, which creates a class1 object which creates a class2 object, which creates a class1 object which creates a class2 object, which...

    Solution: don't do this.

  3. #3
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You've got infinite recursion going on. If you create a class1 object, it creates a class2 object which creates a class1 object which creates a class2 object, which creates a class1 object which creates a class2 object, which creates a class1 object which creates a class2 object, which creates a class1 object which creates a class2 object, which creates a class1 object which creates a class2 object, which...

    Solution: don't do this.
    Please how can I do fix? Can you fix my code please..

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by RichersooN View Post
    Please how can I do fix? Can you fix my code please..
    Better for you to fix your own code. But to solve it, only have one class create an instance of another. Don't have both classes create instances of the other.

    For instance, what you have is this:

    Java Code:
    public class ClassRecursion {
       public static void main(String[] args) {
          Class1 c1 = new Class1();
       }
    }
    
    class Class1 {
       Class2 c2 = new Class2();
       
       public Class1() {
          System.out.println("inside class 1");
       }
    }
    
    class Class2 {
       Class1 c1 = new Class1();
       
       public Class2() {
          System.out.println("inside class 2");
       }
    }
    If you run this, you'll see the same error as in your code and for the very same reason.

    What you want instead is to have one of the classes create an instance of the other, and the other class to receive a reference to the first, something like so:

    Java Code:
    public class ClassRecursion {
       public static void main(String[] args) {
          Class1 c1 = new Class1();
       }
    }
    
    class Class1 {
       Class2B c2b = new Class2B(this); // pass a reference to the current Class1 into Class2B constructor
       
       public Class1() {
          System.out.println("inside class 1");
       }
    }
    
    class Class2B {
       Class1 c1;
       
       public Class2B(Class1 c1) {
          System.out.println("inside class 2B");
          this.c1 = c1;
       }
    }

Similar Threads

  1. [SWING]Adding JButton(class1) to JPanel(class2)
    By equal in forum New To Java
    Replies: 8
    Last Post: 02-20-2011, 01:09 AM
  2. problem of linking text file and java program
    By binweifong in forum New To Java
    Replies: 9
    Last Post: 12-08-2010, 04:06 PM
  3. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM
  4. Linking of exe files
    By archu2friends in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-06-2008, 06:08 AM
  5. <URGENT> problem after linking .jnlp
    By bongia in forum New To Java
    Replies: 14
    Last Post: 11-18-2007, 05:57 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •