0

My program was working perfectly until I used a switch statement in JFrame of 2nd class to be opened from 1st class in same package. I got out of memory error.. related to heap space. I removed that switch statement still same error occured. when there is no linkin between jframes they are running perfectly. but when I link them, 1st frame works and on clicking submit button, it dissappears, probably because in its action I've used this.dispose but it isn't opening 2nd frame and after 5-10 minutes I get out of memory error

Class1(JFrame1)

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            qualification = (String)degree.getSelectedItem( );
    contact_num= Integer.parseInt(num.getText());
    String xyz= (String)d.getSelectedItem();
    date=Integer.parseInt(xyz);
    xyz= (String)m.getSelectedItem();
    month=Integer.parseInt(xyz);
    year=Integer.parseInt(y.getText());
    street= (String)(strt.getText());
    name=(String)nme.getText();
    email= (String)e_mail.getText();
    state= (String)stt.getText();
    city= (String)cty.getText();
    gender= (String)sex.getSelectedItem();
    Sample a=new Sample();
    a.setVisible(true);
       this.dispose();
}

Class2(JFrame2)

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    System.out.print("ab");

}                                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    System.out.print("b");

}                                        

Code for Class2 with switch statement due to which I got that error

public Qualification abc=new Qualification();
    Sample aa= new Sample();
    public String s;
    public void open_sample()
    {
        this.setVisible(true);
        switch(abc.qualification)
        {
            case "BE":
            BE1 a=new BE1(); 
            break;
            case "10th/12th":
            C101 b=new C101();
            b.setVisible(true);
            break;
            case "MBA":
            MBA1 c=new MBA1();
            c.setVisible(true);
            break;
            case "CA":
            CA1 x=new CA1();
            x.setVisible(true);
            break;
            default:

        }
    }

private void sa1ActionPerformed(java.awt.event.ActionEvent evt) {                                   s= evt.getActionCommand();
         open_sample();
 }                                   

    private void sa4ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        s= evt.getActionCommand();
   open_sample();}                                   

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
                      System.out.print("hello");
  }                                        

    private void sa3ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        s= evt.getActionCommand();
       open_sample();
    }                                   

    private void sa2ActionPerformed(java.awt.event.ActionEvent evt) {                                    
           s = evt.getActionCommand();
      open_sample();
    } 
user3819936
  • 35
  • 1
  • 2
  • 11
  • Your above added code shows only a part of your classes (some methods), but i you want to get help, you have to post the hole code. You said something about a switch Statement you added, but i cant so it anywhere in your code above. – Patrick Jul 12 '14 at 07:20
  • @Patrick I removed that switch statement in a hope to eliminate that error.. wait I'll post that too.. and I'm using netbeans. Do I still need to post entire code? – user3819936 Jul 12 '14 at 07:22
  • @Patrick Check out my ques again. I've edited that! – user3819936 Jul 12 '14 at 07:28
  • @user3819936: Seems to be some implementation flaw, but on a side, please have a look at [Use of multiple JFrame Good/Bad practice](http://stackoverflow.com/q/9554636/1057230). Without the relevant code, it is hard to tell, what exactly is being done with the `JFrame`s already referenced, while execution of the program. Try to change the logic to implement [CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html), that will make life easier for you, on this front of managing multiple `JFrame`s – nIcE cOw Jul 12 '14 at 08:19
  • @nIcEcOw okay I'll read that thanx :) – user3819936 Jul 12 '14 at 08:32
  • @user3819936: You're MOST WELCOME and KEEP SMILING :-) Though out of curiousity, how you intend to deal with the `JFrame`s you created in that `switch` block, since the reference, is local to that block? Hope you have implemented means to deal with them somewhere in the code. Why not create a single __super__ class `Course` and then you can use single reference for all __base__ classes like `MBA/BE/C101/CA` – nIcE cOw Jul 12 '14 at 08:39
  • @nIcEcOw I've asked this ques now.. I don't know how to deal with this see this.... http://stackoverflow.com/questions/24711264/using-switch-statement-to-provide-action-to-jbuttons-java – user3819936 Jul 12 '14 at 08:51

2 Answers2

0

The “java.lang.OutOfMemoryError: Java heap space” error is triggered when you try to add more data into the heap space area in memory, but the size of this data is larger than the JVM can accommodate in the Java heap space.

In many cases you can get away by just increasing the heap space by specifying (or increasing if present) -Xmx parameter in your startup script, for example -Xmx1024m.

Flexo
  • 87,323
  • 22
  • 191
  • 272
Ivo
  • 444
  • 3
  • 7
0

My first thought was:

final Sample a = new Sample();
SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        a.setVisible(true);
    }
});
this.dispose();

This is in general a good practise, fast handling an event on the event queue handling thread, and doing the real work a bit later.

However one error seems that the code is using fields instead of local variables, and seem to be used inside the Sample constructor. So Sample is an embedded class? Then a this.dispose() will not free the class itself, as long as Sample is not freed it maintains a JFrame1.this.

Not sure whether this helps, but a bit of code rewriting seems appropiate.

Also try

setDefaultCloseOperation(DISPOSE_ON_CLOSE); // Init
...
setVisible(false); // Instead of dispose()
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138