Results 1 to 5 of 5
  1. #1
    ganchinyong is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default Error - Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap

    I have faced this error and i could find the solution. Could anyone please solve it? Urgent. Thanks
    Below is my coding:

    Java Code:
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import javax.swing.JFileChooser;
    import javax.swing.JOptionPane;
    
    public class toolFrame extends javax.swing.JFrame {
        
        final JFileChooser fc = new JFileChooser();   
        private String output = "Result.txt";
        File file = new File(output);
        TextFile tf = new TextFile();
    
    
        /** Creates new form toolFrame */
        public toolFrame() {
            super("LockText Text Encryption Tool");
            initComponents();
        }
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jLabel1 = new javax.swing.JLabel();
            jTextField1 = new javax.swing.JTextField();
            jLabel2 = new javax.swing.JLabel();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jButton3 = new javax.swing.JButton();
            jButton4 = new javax.swing.JButton();
            jLabel3 = new javax.swing.JLabel();
            jLabel4 = new javax.swing.JLabel();
            jButton5 = new javax.swing.JButton();
            jLabel5 = new javax.swing.JLabel();
            jScrollPane1 = new javax.swing.JScrollPane();
            jTextArea1 = new javax.swing.JTextArea();
            jScrollPane2 = new javax.swing.JScrollPane();
            jTextArea2 = new javax.swing.JTextArea();
            jButton6 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jLabel1.setText("Enter password(secret key):");
    
            jLabel2.setText("Contents retrieved from file:");
    
            jButton1.setText("Open file");
            jButton1.setActionCommand("openFileButton");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jButton2.setText("Encrypt");
            jButton2.setActionCommand("encryptButton");
    
            jButton3.setText("Decrypt");
            jButton3.setActionCommand("decryptButton");
    
            jButton4.setText("Save file");
            jButton4.setActionCommand("saveFileButton");
            jButton4.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton4ActionPerformed(evt);
                }
            });
    
            jLabel3.setText("Output data:");
    
            jLabel4.setText("(*must ** digits long)");
    
            jButton5.setText("Close");
            jButton5.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton5ActionPerformed(evt);
                }
            });
    
            jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dmptextencryptiontool/resources/icon.jpg"))); // NOI18N
    
            jTextArea1.setColumns(20);
            jTextArea1.setRows(5);
            jScrollPane1.setViewportView(jTextArea1);
    
            jTextArea2.setColumns(20);
            jTextArea2.setRows(5);
            jScrollPane2.setViewportView(jTextArea2);
    
            jButton6.setText("Clear text area");
            jButton6.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton6ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel2)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE)
                            .addGap(16, 16, 16)
                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel1)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jLabel4))
                                .addComponent(jLabel3)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(78, 78, 78))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(36, 36, 36)
                                    .addComponent(jLabel5)
                                    .addGap(16, 16, 16))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(33, 33, 33)))))
                    .addGap(10, 10, 10))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jLabel2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel1)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel4))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jLabel3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 386, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(4, 4, 4)))
                    .addGap(23, 23, 23))
            );
    
            pack();
        }// </editor-fold>                        
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // button 1(open)
            //JOptionPane.showMessageDialog(null,"Opening file");
            tf.viewFile();
    }
    
    public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                   TextFile tf = new TextFile();               
                    new toolFrame().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JButton jButton4;
        private javax.swing.JButton jButton5;
        private javax.swing.JButton jButton6;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JScrollPane jScrollPane2;
        public javax.swing.JTextArea jTextArea1;
        private javax.swing.JTextArea jTextArea2;
        private javax.swing.JTextField jTextField1;
    }

    Java Code:
    import java.io.File;
    import java.util.*;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import javax.swing.JFileChooser;
    import javax.swing.JOptionPane;
    
    
    public class TextFile extends toolFrame{
        private String fileName;
        File file = new File("output.txt");
        public TextFile(){
            fileName = "";
        }
        public void setFilename(String fn){
            fn = fileName;
        }
    
        public String getFilename(){
            return fileName;
        }
    
        public void viewFile(){
            JOptionPane.showMessageDialog(null,"File is opened.");
            try{
                JFileChooser loadFile = new JFileChooser();//new dialog
                 File selectedFile;
                 BufferedReader in;
                 FileReader reader = null;
    
        //opens dialog if button clicked
        if (loadFile.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
        {
          //gets file from dialog
          selectedFile = loadFile.getSelectedFile();
          //makes sure files can be processed before proceeding
          if (selectedFile.canRead() && selectedFile.exists())
          {
          reader = new FileReader(selectedFile);
          }
        }
    
       in = new BufferedReader(reader);
    
        //inputLine recieves file text
        String inputLine = in.readLine();
        int lineNo = 0;
        while(inputLine!=null)
        {
          //LineNumber isn't needed, but it adds a line count on the left, nice
          lineNo++;
          //StringTokenizer tokenizer = new StringTokenizer(inputLine);
    
          //displays text file in the text area
          jTextArea1.append(lineNo+") "+inputLine+"\n");
          //next line in File opened
          inputLine = in.readLine();
        }
        //close stream, files stops loading
        in.close();
      }
      //catches input/output exceptions and all subclasses exceptions
      catch(IOException e)
      {
        jTextArea1.append("Error When Processing File:\n" + e.getMessage()+"\n");
      }
      //catches nullpointer exception, file not found
      catch(NullPointerException e)
      {
        jTextArea1.append("Open File Is Cancelled:\n" + e.getMessage()+"\n");
      }
            
    }
    }
    Moderator Edit: Code tags added
    Attached Thumbnails Attached Thumbnails Error - Exception in thread &quot;AWT-EventQueue-0&quot; java.lang.OutOfMemoryError: Java heap-error.jpg  
    Last edited by Fubarable; 06-02-2010 at 10:43 PM. Reason: Moderator Edit: Code tags added

  2. #2
    ganchinyong is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    i couldnt find the solution**
    and the image attached is the error message appeared after few minutes it runs.
    My problem is when I run it, there is nothing come out but the coding part got no problem when compiled.
    Any helps are appreciated so much.

  3. #3
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Hi,

    Firstly you should apply code formatting to any code you send. It makes it much easier to read ^_^
    You can do this by highlighting code and pressing the "#" symbol top right.

    Secondly, if you read the message it tells you have run out of memory.

    This is caused by several things. Firstly, you might not be letting the Garbage Collector run its job - make sure you delete all pointers to unecessary objects.

    Secondly, it could be because you genuinly need a lot of memory - for a large file or picture. The default heap size for java is 128MB. If you think you need more than this, you can use from commandline:
    Java Code:
        // Sets the maximum size to 256MB
        java -Xmx256m <yourprogramname>
        // Sets the minimum size to 128MB and maximum size to 256MB
        java -Xms128m -Xmx256 <yourprogramname>
    The minimum size is not necessary, but if you need lots of memory it is recommended (it is faster).

    Only use this code if you genuinly need it, if you have a memory leak then it only hides it for longer!

    Your code looks like your using netbeans.
    If thats the case, then you can right click on your project -> Properties -> Categories -> Run.
    Put the code into the vmOptions box.

    Hope this helps and good luck.
    Berkeleybross

  4. #4
    ganchinyong is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    First, Berkeleybross thanks you so muchhh for replying.
    Yes. I'm using the Netbeans.
    I have tried your suggestion and the problem of out of memory is solved. It is helpful. Thanks dude!
    However, there is another error comes out as the image attached.
    Can you tell me whats the problem?


    Regards,
    GAN
    Attached Thumbnails Attached Thumbnails Error - Exception in thread &quot;AWT-EventQueue-0&quot; java.lang.OutOfMemoryError: Java heap-error.jpg  

  5. #5
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    I think you misunderstood my directions for netbeans usage.

    The code for vmOptions should be something like
    Java Code:
    -Xmx256m -Xms128m
    If that doesnt fix it, please can you explain more about the problem i.e. when does it occur, what did you do that caused it etc

    Berkeleybross

Similar Threads

  1. Replies: 3
    Last Post: 04-30-2010, 07:44 PM
  2. Replies: 4
    Last Post: 06-07-2009, 12:59 AM
  3. Replies: 10
    Last Post: 05-11-2009, 11:56 AM
  4. Replies: 3
    Last Post: 04-22-2009, 08:25 PM
  5. Replies: 14
    Last Post: 06-12-2008, 08:36 AM

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
  •