Results 1 to 5 of 5
  1. #1
    gisler is offline Member
    Join Date
    Apr 2009
    Posts
    26
    Rep Power
    0

    Default frame constructor

    Hi,

    I have created a java frame using netbeans. In my constructor i am trying to create an instance of another class. When i do this i get an error and can't figure out why it will not let me create the instance. The class is called Connect.

    Java Code:
    public class Client extends javax.swing.JFrame {
    
        JFileChooser fc;
        DefaultListModel listModel;
        Connect connection; 
        /** Creates new form Client */
        public Client() {
    
            initComponents();
            fc = new JFileChooser();
            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            Connect connection = new Connect();
        }
                                        
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new Client().setVisible(true);
    
                }
            });
        }
    
    }
    Note: i have removed all the generated code from the compiler along with code that i am sure is not relevant. If this is not enough please let me know.

    many thanks

    Gisler

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    public class Client extends javax.swing.JFrame {
        JFileChooser fc;
        DefaultListModel listModel;
        Connect connection; // member variable in class scope
    
        public Client() {
            initComponents();
            fc = new JFileChooser();
            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            // Note: [i]connection[/i] below is a local variable and hides the
            // member variable, in class scope above, whose value remains null.
            // One possibility:
            // Does the Connect class have a no–argument constructor?
            // Java provides one unless you declare a constructor that
            // does have one or more arguments. If so, you must explicitly
            // declare a no–argument constructor for the class.
            Connect connection = new Connect();
        }

  3. #3
    gisler is offline Member
    Join Date
    Apr 2009
    Posts
    26
    Rep Power
    0

    Default

    Hi,

    if i understand correctly my connect class does have a no-argument constructor. It looks like this...
    Java Code:
    public Connect(){
    
    }
    the actual frame is not showing. i just get an error saying "Exception in thread 'Awt-EventQueue-0'"

    thanks for the help

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

  5. #5
    gisler is offline Member
    Join Date
    Apr 2009
    Posts
    26
    Rep Power
    0

    Default

    not quite sure i undersand what a stack trace is so i hope this is correct.

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
            at java.lang.reflect.Array.newArray(Native Method)
            at java.lang.reflect.Array.newInstance(Array.java:52)
            at sun.awt.EventListenerAggregate.add(EventListenerAggregate.java:73)
            at java.beans.PropertyChangeSupport.addPropertyChangeListener(PropertyChangeSupport.java:75)
            at javax.swing.UIManager.addPropertyChangeListener(UIManager.java:1152)
            at javax.swing.filechooser.FileSystemView.getFileSystemView(FileSystemView.java:64)
            at javax.swing.JFileChooser.setup(JFileChooser.java:357)
            at javax.swing.JFileChooser.<init>(JFileChooser.java:333)
            at javax.swing.JFileChooser.<init>(JFileChooser.java:286)
            at Client.<init>(Client.java:40)
            at Connect.<init>(Connect.java:10)
            at Client.<init>(Client.java:42)
            at Connect.<init>(Connect.java:10)
            at Client.<init>(Client.java:42)
            at Connect.<init>(Connect.java:10)        at Connect.<init>(Connect.java:10)
    the code that causes this error is the second line down in my main method.
    Java Code:
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Client().setVisible(true);
    
             }
            });
        }
    hope that helps. thanks for the help once again

    gisler

Similar Threads

  1. Replies: 7
    Last Post: 11-02-2009, 07:31 PM
  2. Constructor
    By Sarinam in forum AWT / Swing
    Replies: 1
    Last Post: 06-19-2008, 09:03 AM
  3. Replies: 0
    Last Post: 12-19-2007, 10:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 10:01 AM
  5. Constructor Help
    By bluegreen7hi in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 06:44 AM

Posting Permissions

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