Results 1 to 5 of 5
  1. #1
    tiad123 is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Default Exception issues...

    Hi, I am having an exception issue here. If anyone could point me in the right direction here I would be very grateful. It seems like it has something to do with the version of the JRE that I use? I really don't understand where to even begin fixing this. Is there anywhere you would recommend I look to find this particular exception?

    Java Code:
    import javax.swing.JFrame;
    
    public class myMessin{
    	public static void main (String args[]){
    		
    		GUI2 go = new GUI2();
    		go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		go.setSize(300,200);
    		go.setVisible(true);
    		
    	GUI2 file = new GUI2("arrayRecord.txt");
    	file.showFile();
    	
    	
    	}
    }
    Java Code:
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.*;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.*;
    
    public class GUI2 extends JFrame {
    	
    	private JButton first;
        private JButton prev;
        private JButton next;
        private JButton last;
        public int index = 0;
        private List<String> lines;
        private int currentIndex;
        private JFrame textFrame;
    
        public GUI2() {
            super("Inventory Program Part 5");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            lines = new ArrayList<String>();
            setLayout(new FlowLayout());
    
            first = new JButton("First");
            add(first);
    
            prev = new JButton("Previous");
            add(prev);
    
            next = new JButton("Next");
            add(next);
    
            last = new JButton("Last");
            add(last);
    
            HandlerClass handler = new HandlerClass();
            first.addActionListener(handler);
            prev.addActionListener(handler);
            next.addActionListener(handler);
            last.addActionListener(handler);
        }
    
        public GUI2(String arrayRecord) {
            this();
    
            try {
                BufferedReader reader = new BufferedReader(new FileReader(
                            arrayRecord));
                String line = "";
    
                while ((line = reader.readLine()) != null) {
                    lines.add(line);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    
        public void showFile() {
            for (int i = 0; i < lines.size(); i++) {
                System.out.println(lines.get(i));
            }
        }
    
        public static void main(final String[] args) {
            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        GUI2 f = new GUI2(args[0]);
                        f.setSize(200, 200);
                        f.setVisible(true);
                    }
                });
            } catch (Exception e) {
               System.out.println("Error");
            }
        }
    
        private class HandlerClass implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                textFrame = new JFrame("Inventory");
                textFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                textFrame.getContentPane();
    
                JTextArea textArea = new JTextArea();
                textFrame.add(textArea);
    
                Object button = event.getSource();
    
                if (first == button) {
                    currentIndex = 0;
                } else if ((prev == button) && (currentIndex > 0)) {
                    --currentIndex;
                } else if (next == button) {
                    ++currentIndex;
    
                    // Wrap
                    if (currentIndex == lines.size()) {
                        currentIndex = 0;
                    }
                } else if (last == button) {
                    currentIndex = lines.size() - 1;
                }
    
                textArea.setText(lines.get(currentIndex));
                textFrame.setSize(200, 150);
                textFrame.setVisible(true);
            }
        }
    }

    My errors are:


    Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
    at java.util.ArrayList.RangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at GUI2$HandlerClass.actionPerformed(GUI2.java:112)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)


    Thanks in advance for any help :)

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    You are trying to access an empty arraylist

  3. #3
    tiad123 is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Default Thanks

    Is that possibly because I deleted the class that loaded the original array into the file? or because I didn't read the file correctly?

  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 tiad123 View Post
    Is that possibly because I deleted the class that loaded the original array into the file?
    Once the file is created, I don't see how deleting the class that created it will effect the file in any way, do you?

    or because I didn't read the file correctly?
    Some clever debugging on your part will see if this is so, and likely how to fix it.

  5. #5
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    It is because you made two instances of the class, go and file. Go has an empty array list and shows button, file has a full array list and shows no buttons. So when you hit a button, it takes the empty list that belongs to go, and tries to perform a get() on it, resulting in an exception.

Similar Threads

  1. Replies: 4
    Last Post: 10-25-2010, 07:42 PM
  2. jdk issues
    By artemff in forum New To Java
    Replies: 3
    Last Post: 01-02-2010, 03:18 AM
  3. Replies: 1
    Last Post: 03-08-2008, 06:41 PM
  4. Issues with Jva I.O
    By Annatar01 in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 01:16 AM
  5. Issues with a while loop
    By Lang in forum New To Java
    Replies: 4
    Last Post: 11-09-2007, 09:01 PM

Posting Permissions

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