Results 1 to 4 of 4
  1. #1
    AndrewSD is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default I need help with my program

    Well my program is supposed to compress files that are choosen by the user but I always get this one error with my array that I am using:
    Java Code:
    java.lang.ArrayIndexOutOfBoundsException: 0
            at azipper.azipperWindow.calculateStringArray(azipperWindow.java:197)
            at azipper.azipperWindow.jButton4ActionPerformed(azipperWindow.java:220)
            at azipper.azipperWindow.access$200(azipperWindow.java:21)
            at azipper.azipperWindow$3.actionPerformed(azipperWindow.java:78)
            at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
            at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
            at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
            at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
            at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
            at java.awt.Component.processMouseEvent(Component.java:6267)
            at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
            at java.awt.Component.processEvent(Component.java:6032)
            at java.awt.Container.processEvent(Container.java:2041)
            at java.awt.Component.dispatchEventImpl(Component.java:4630)
            at java.awt.Container.dispatchEventImpl(Container.java:2099)
            at java.awt.Component.dispatchEvent(Component.java:4460)
            at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
            at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
            at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
            at java.awt.Container.dispatchEventImpl(Container.java:2085)
            at java.awt.Window.dispatchEventImpl(Window.java:2478)
            at java.awt.Component.dispatchEvent(Component.java:4460)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    Here is my code:
    Java Code:
    
    
    /*
     * azipperWindow.java
     *
     * Created on Jan 21, 2011, 7:56:24 PM
     */
    
    package azipper;
    import java.io.File;
    import javax.swing.*;
    import java.io.*;
    import java.util.zip.*;
    
    
    
    /**
     *
     * @author Andrew Delgadillo
     */
    public class azipperWindow extends javax.swing.JFrame {
    
        DefaultListModel model = new DefaultListModel();
       
        
    
        /** Creates new form azipperWindow */
        public azipperWindow() {
            initComponents();
            
            
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jLabel1 = new javax.swing.JLabel();
            jButton4 = new javax.swing.JButton();
            jSeparator1 = new javax.swing.JSeparator();
            jLabel2 = new javax.swing.JLabel();
            jButton5 = new javax.swing.JButton();
            jScrollPane1 = new javax.swing.JScrollPane();
            jList1 = new javax.swing.JList();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("A-Zipper ");
            setName("mainWindow"); // NOI18N
    
            jButton1.setText("Add File");
            jButton1.setName("addFile"); // NOI18N
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jButton2.setText("Delete File");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
    
            jLabel1.setText("Compress File(s)");
    
            jButton4.setText("Compress File(s)");
            jButton4.setName("compress"); // NOI18N
            jButton4.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton4ActionPerformed(evt);
                }
            });
    
            jLabel2.setText("Uncompress File");
    
            jButton5.setText("Decompress File");
            jButton5.setName("compress"); // NOI18N
    
            jScrollPane1.setViewportView(jList1);
    
            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, false)
                        .addComponent(jLabel1)
                        .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(18, 18, 18)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
                    .addContainerGap())
                .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 426, Short.MAX_VALUE)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel2)
                    .addContainerGap(339, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jButton5)
                    .addContainerGap(307, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton4))
                        .addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE))
                    .addGap(18, 18, 18)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jLabel2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jButton5)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
    
            jButton2.getAccessibleContext().setAccessibleName("");
    
            pack();
        }// </editor-fold>                        
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
          
            jList1.setModel(model);
    
            final JFileChooser fc = new JFileChooser();
            fc.setMultiSelectionEnabled(false);
    
            int returnVal = fc.showOpenDialog(azipperWindow.this);
    
    
    
    
    
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
    
                model.addElement(file.getAbsolutePath());
               
                
    
    
    
                
    
               
                
                
    
            }
    
    
        }                                        
    
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            int index = jList1.getSelectedIndex();
                   
            if(index != -1) {
            model.remove(index);
    
            }
                
    
            
    
    
    
    
        }                                        
    
    
        private String[] calculateStringArray() {
           int size = ((model.getSize()) - 1);
            
            String files[] = new String[size];
    
            for(int i = 0; i < model.getSize(); i++) {
                files[i] = String.valueOf(model.get(i));
    
             }
    
            return files;
        }
        private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        JFileChooser cf = new JFileChooser();
        cf.setDialogTitle("Choose where to save archive...");
        
        int returnvalue = cf.showDialog(null, "Save Archive");
        File zip = cf.getSelectedFile();
            if(returnvalue == JFileChooser.APPROVE_OPTION) {
                final int BUFFER = 2048;
                try {
             BufferedInputStream origin = null;
             FileOutputStream dest = new
               FileOutputStream(zip.getAbsolutePath()+".zip");
             ZipOutputStream out = new ZipOutputStream(new
               BufferedOutputStream(dest));
             //out.setMethod(ZipOutputStream.DEFLATED);
             byte data[] = new byte[BUFFER];
             // get a list of files from current directory
             String[] files = calculateStringArray();
             
            
             
    
             for (int i=0; i<files.length; i++) {
                
                FileInputStream fi = new
                  FileInputStream(files[i]);
                origin = new
                  BufferedInputStream(fi, BUFFER);
                ZipEntry entry = new ZipEntry(files[i]);
                out.putNextEntry(entry);
                int count;
                while((count = origin.read(data, 0,
                  BUFFER)) != -1) {
                   out.write(data, 0, count);
                }
                origin.close();
             }
             out.close();
          } catch(Exception e) {
             e.printStackTrace();
          }
    
                
            }
    
    
    
    
        }                                        
    
    
    
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
             try {
    	    
            UIManager.setLookAndFeel(
                UIManager.getSystemLookAndFeelClassName());
        }
        catch (UnsupportedLookAndFeelException e) {
           JOptionPane.showMessageDialog(null, "Error");
        }
        catch (ClassNotFoundException e) {
          JOptionPane.showMessageDialog(null, "Error");
        }
        catch (InstantiationException e) {
           JOptionPane.showMessageDialog(null, "Error");
        }
        catch (IllegalAccessException e) {
           JOptionPane.showMessageDialog(null, "Error");
        }
    
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new azipperWindow().setVisible(true);
                    
                }
            });
    
        
        }
    
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton4;
        private javax.swing.JButton jButton5;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JList jList1;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JSeparator jSeparator1;
        // End of variables declaration                   
    
    
    
    }

    My problem is when I press the compress files button it always gives me a array out of bounds expetion what should I do to fix this?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    private String[] calculateStringArray() {
        int size = ((model.getSize()) - 1);
            
        String files[] = new String[size];
    
        for(int i = 0; i < model.getSize(); i++) {
            files[i] = String.valueOf(model.get(i));
    
        }
    
        return files;
    }

    This code sets up an array with model.getSize()-1 places in it. But then you use a for loop that will access up to model.getSize() places. Basically you should make the array larger:

    Java Code:
    int size = model.getSize();
    String files[] = new String[size];

  3. #3
    AndrewSD is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    Thanks that fixed the problem, I would have never suspected it, I am pretty new to java and it is just a hobby. Could I ask another question? When I debug it and press the compress files button it creates a .zip file but it has nothing in it. I followed the tutorials that is on oracle.com. I just don't know what is wrong with it!

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You're welcome. You'll quickly get used to the fact than an array declared as new Whatever[n] has index values that run from 0 to n-1. Therefore the appropriate for loop would be for(int i=0; i<n; i++).

    As regards the file not having any content... I have run your program a few times and noticed that although WindowsExplorer showed no contents the .zip files were different sizes. Choosing "Extract All..." from the File menu brought up (eventually) a "Windows has blocked access to these files" message.

    Anyway the .zip files opened OK using WinZip so I think the problem may lie with Windows faulty security rather than your program.

    You might try checking the properties of the .zip file. If you see an "Unblock" option near the bottom, choose it.

    Others may be able to provide better help here. I have seen this before, but can't remember the details.

Similar Threads

  1. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  2. Execute A program from a Program!
    By Moncleared in forum Advanced Java
    Replies: 2
    Last Post: 02-22-2009, 04:17 PM
  3. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 08:24 AM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 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
  •