Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java run time error...

Options
  • 12-12-2006 5:17pm
    #1
    Closed Accounts Posts: 49


    Hi,

    im doing a project for college and i keep getting this error:

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 0


    Now that all well and good if i could understand how it is happening...here is the piece of code where its crashing:
    list.addListSelectionListener(
                        new ListSelectionListener(){
    				public void valueChanged(ListSelectionEvent e)
    				{
    					int i = list.getSelectedIndex();
    					pickitem.delete(0, pickitem.length());				
    					pickitem.append(items.elementAt(i));
    					
    				}
    			}
    		);
    


    any suggestions?

    cheers


Comments

  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    list.addListSelectionListener(
                        new ListSelectionListener(){
    				public void valueChanged(ListSelectionEvent e)
    				{
    					int i = list.getSelectedIndex();
    						
    					[b]pickitem.append(items.elementAt(i));
                                            pickitem.delete(0, pickitem.length());[/b]
    					
    				}
    			}
    		);
    

    I've switched the lines of code in bold around from the original. It's my guess that deleting an item will resize the array, hence the error. You want to perform the delete last.


  • Closed Accounts Posts: 49 boyracer87


    "pickitem" is actually a stringBuffer...if i put delete after it im only going to delete the string i put in it in the begining...heres a bigger block of the code...(Theres 700 line to the whole class so i dont realy want ot put up the whole thing!!):
    public RaysZork() 
        {    
    	    super("Rays Zork");	
    	    createRooms();
    	    setItems();
            parser = new Parser();	
    		
    		
    		container = getContentPane();
    		container.setLayout(gbLayout);
    
    		JMenuBar bar = new JMenuBar();
    		JMenu menu = new JMenu("File");
    		JMenuItem help1 = new JMenuItem("Help");
    		help1.addActionListener (new ActionListener() {
    			public void actionPerformed (ActionEvent events)
    			{
    				JOptionPane.showMessageDialog(null, "The aim of the game is to find your way around\nall the rooms and bring all the movable objects\nback to the lobby in the quickest time possable.\nYou cannot pick up too many items at the one time.\nThere is a weight counter on the bottom of the page with the current weight.\nEvery time you pick up an item, this will increase.\n\nYou have a maximum weight of 42 grams.\n\nThere is a magic room to the south of the lobby.\nThis room will take you to a random room in the game.\n\n");
    			}
    		}
    		);
    		JMenuItem quit = new JMenuItem("Quit");
    		quit.addActionListener (new ActionListener() {
    			public void actionPerformed (ActionEvent events)
    			{
    				//JOptionPane.showMessageDialog(null,"Your time was: ");
    				System.exit(0);
    			}
    		}
    		);
    		menu.add(help1);
    		menu.add(quit);
    		bar.add(menu);
    		
    		final JList list = new JList();
    		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		list.addListSelectionListener(
    			new ListSelectionListener(){
    				public void valueChanged(ListSelectionEvent e)
    				{
    					int i = list.getSelectedIndex();
    					//JOptionPane.showMessageDialog(null,i);
    					pickitem.delete(0, pickitem.length());
                                                                      pickitem.append(items.elementAt(i));
    									
    					
    					//JOptionPane.showMessageDialog(null,pickitem);				
    					//getPickItem(pickitem);
    				}
    			}
    		);
    		final JLabel pic = new JLabel();
    		
    		pic.setIcon(roompic1);
    		list.setListData(listItems());
    		gbCons.fill = GridBagConstraints.BOTH;
    		
    		JButton north = new JButton("North");
    		north.addActionListener (new ActionListener() {
    			public void actionPerformed (ActionEvent events)
    			{
    				goRoom("north");
    				if(currentRoom == room1){
    					pic.setIcon(roompic1);
    					list.setListData(listItems());
    				}else if(currentRoom == room2){
    					pic.setIcon(roompic2);
    					list.setListData(listItems());
    				}else if(currentRoom == room3){
    					pic.setIcon(roompic3);
    					list.setListData(listItems());
    				}else if(currentRoom == room4){
    					pic.setIcon(roompic4);
    					list.setListData(listItems());
    				}else{
    					JOptionPane.showMessageDialog(null, "Wha");
    				}
    					   
    			}
    		}
    		);
    		
    		
    		JButton south = new JButton("South");
    		south.addActionListener (new ActionListener() {
    			public void actionPerformed (ActionEvent events)
    			{
    				goRoom("south");
    				if(currentRoom == room1){
    					pic.setIcon(roompic1);
    					list.setListData(listItems());
    				}else if(currentRoom == room2){
    					pic.setIcon(roompic2);
    					list.setListData(listItems());
    				}else if(currentRoom == room3){
    					pic.setIcon(roompic3);
    					list.setListData(listItems());
    				}else if(currentRoom == room4){
    					pic.setIcon(roompic4);
    					list.setListData(listItems());
    				}else{
    					JOptionPane.showMessageDialog(null, "Wha");
    				}
    			
    			}
    			
    			
    		}
    		);
    		
    		
    		JButton east = new JButton("East");
    		east.addActionListener (new ActionListener() {
    			public void actionPerformed (ActionEvent events)
    			{
    				goRoom("east");
    				 if(currentRoom == room1){
    					pic.setIcon(roompic1);
    					list.setListData(listItems());
    				}else if(currentRoom == room2){
    					pic.setIcon(roompic2);
    					list.setListData(listItems());
    				}else if(currentRoom == room3){
    					pic.setIcon(roompic3);
    					list.setListData(listItems());
    				}else if(currentRoom == room4){
    					pic.setIcon(roompic4);
    					list.setListData(listItems());
    				}else{
    					JOptionPane.showMessageDialog(null, "Wha");
    				}
    			
    			}
    		}
    		);
    		
    		
    		JButton west = new JButton("West");
    		west.addActionListener (new ActionListener() {
    			public void actionPerformed (ActionEvent events)
    			{
    				goRoom("west");
    				if(currentRoom == room1){
    					pic.setIcon(roompic1);
    					list.setListData(listItems());
    				}else if(currentRoom == room2){
    					pic.setIcon(roompic2);
    					list.setListData(listItems());
    				}else if(currentRoom == room3){
    					pic.setIcon(roompic3);
    					list.setListData(listItems());
    				}else if(currentRoom == room4){
    					pic.setIcon(roompic4);
    					list.setListData(listItems());
    				}else{
    					JOptionPane.showMessageDialog(null, "Wha");
    				}
    			
    			}
    			
    		}
    		
    		);
    		
    		
    		JButton back = new JButton ("Back");
    		back.addActionListener (new ActionListener() {
    			public void actionPerformed (ActionEvent events)
    			{
    				currentRoom = previousRoom;
    				if(currentRoom == room1){
    					pic.setIcon(roompic1);
    					list.setListData(listItems());
    				}else if(currentRoom == room2){
    					pic.setIcon(roompic2);
    					list.setListData(listItems());
    				}else if(currentRoom == room3){
    					pic.setIcon(roompic3);
    					list.setListData(listItems());
    				}else if(currentRoom == room4){
    					pic.setIcon(roompic4);
    					list.setListData(listItems());
    				}else{
    					JOptionPane.showMessageDialog(null, "Wha");
    				}
    			}
    		}
    		);
    		
    		JButton pickup = new JButton("Pick Up");
    		pickup.addActionListener (new ActionListener() {
    			public void actionPerformed (ActionEvent events)
    			{
    				//JOptionPane.showMessageDialog(null, pickitem);
    				getItem(pickitem);
    				
    				list.setListData(listItems());
    				
    			}
    		}
    	    ); 	
    		    
    	    
    		JButton drop = new JButton("Drop");
    		JButton dropAll = new JButton ("Drop all");		
    	
    		JLabel label2 = new JLabel("         ");
    		JList list2 = new JList(items);
    		JLabel roomitems = new JLabel("Items in room:");
    		JLabel handitems = new JLabel("Items in hand:");
    		
    		gbCons.fill = GridBagConstraints.BOTH;
    		
    		gbCons.weightx=0;
    		gbCons.weighty=0;
    		addComponent(north, 0, 1, 1, 1);
    		addComponent(south, 2, 1, 1, 1);
    		addComponent(back, 1, 1, 1, 1);
    		addComponent(east, 1, 2, 1, 1);
    		addComponent(west, 1, 0, 1, 1);
    		addComponent(label2, 3, 0, 1, 1);	
    		addComponent(pickup, 4, 0, 3, 1);
    		addComponent(drop, 5, 0, 3, 1);
    		addComponent(dropAll, 6, 0, 3, 1);
    		addComponent(roomitems, 7,0,3,1);
    		addComponent(handitems,9,0,3,1);
    		
    		gbCons.weightx=1000;
    		gbCons.weighty=1;
    		
    		addComponent(list, 8, 0, 3, 1);
    		addComponent(list2, 10, 0, 3, 1);
    		addComponent(pic, 0, 5, 0, 0);
    		
    		setJMenuBar(bar);
    		setSize(800,500);
    		setVisible(true);
    		setResizable(false);
    		
        }
        
    
    
    


  • Moderators, Science, Health & Environment Moderators Posts: 10,079 Mod ✭✭✭✭marco_polo


    Can you print a bigger stack trace so we can see what is causing the error.
    Line numbers would be good too


  • Closed Accounts Posts: 49 boyracer87


    Heres the stack trace:

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 0
    at java.util.Vector.elementAt(Unknown Source)
    at RaysZork$3.valueChanged(RaysZork.java:107)
    at javax.swing.JList.fireSelectionValueChanged(Unknown Source)
    at javax.swing.JList$ListSelectionHandler.valueChanged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
    at javax.swing.DefaultListSelectionModel.changeSelection(Unknown Source)
    at javax.swing.DefaultListSelectionModel.changeSelection(Unknown Source)
    at javax.swing.DefaultListSelectionModel.setSelectionInterval(Unknown Source)
    at javax.swing.JList.setSelectionInterval(Unknown Source)
    at javax.swing.plaf.basic.BasicListUI$Handler.adjustSelection(Unknown Source)
    at javax.swing.plaf.basic.BasicListUI$Handler.adjustFocusAndSelection(Unknown Sourc
    at javax.swing.plaf.basic.BasicListUI$Handler.mousePressed(Unknown Source)
    at java.awt.AWTEventMulticaster.mousePressed(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(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown 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.pumpOneEventForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)


    and code with some line numbers:
    64 public RaysZork() 
        {    
    	    super("Rays Zork");	
    	    createRooms();
    	    setItems();
            parser = new Parser();	
    		
    		
    		container = getContentPane();
    		container.setLayout(gbLayout);
    
    		JMenuBar bar = new JMenuBar();
    		JMenu menu = new JMenu("File");
    		JMenuItem help1 = new JMenuItem("Help");
    		help1.addActionListener (new ActionListener() {
    			public void actionPerformed (ActionEvent events)
    			{
    				JOptionPane.showMessageDialog(null, "The aim of the game is to find your way around\nall the rooms and bring all the movable objects\nback to the lobby in the quickest time possable.\nYou cannot pick up too many items at the one time.\nThere is a weight counter on the bottom of the page with the current weight.\nEvery time you pick up an item, this will increase.\n\nYou have a maximum weight of 42 grams.\n\nThere is a magic room to the south of the lobby.\nThis room will take you to a random room in the game.\n\n");
    			}
    		}
    		);
    		JMenuItem quit = new JMenuItem("Quit");
    		quit.addActionListener (new ActionListener() {
    			public void actionPerformed (ActionEvent events)
    			{
    				//JOptionPane.showMessageDialog(null,"Your time was: ");
    				System.exit(0);
    			}
    		}
    		);
    		menu.add(help1);
    		menu.add(quit);
    		bar.add(menu);
    		
    		final JList list = new JList();
    		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		list.addListSelectionListener(
    			new ListSelectionListener(){
    				public void valueChanged(ListSelectionEvent e)
    	103			{
    	104				int i = list.getSelectedIndex();
    	105				//JOptionPane.showMessageDialog(null,i);
    	106				pickitem.delete(0, pickitem.length());
    	107				pickitem.append(items.elementAt(i));
    	108				
    	109				
    					
    					
    					
    					//JOptionPane.showMessageDialog(null,pickitem);				
    					//getPickItem(pickitem);
    				}
    			}
    		);
    		final JLabel pic = new JLabel();
    		
    		pic.setIcon(roompic1);
    		list.setListData(listItems());
    		gbCons.fill = GridBagConstraints.BOTH;
    		
    		JButton north = new JButton("North");
    		north.addActionListener (new ActionListener() {
    			public void actionPerformed (ActionEvent events)
    			{
    				goRoom("north");
    				if(currentRoom == room1){
    					pic.setIcon(roompic1);
    					list.setListData(listItems());
    				}else if(currentRoom == room2){
    					pic.setIcon(roompic2);
    					list.setListData(listItems());
    				}else if(currentRoom == room3){
    					pic.setIcon(roompic3);
    					list.setListData(listItems());
    				}else if(currentRoom == room4){
    					pic.setIcon(roompic4);
    					list.setListData(listItems());
    				}else{
    					JOptionPane.showMessageDialog(null, "Wha");
    				}
    					   
    			}
    		}
    		);
    		
    		
    		JButton south = new JButton("South");
    		south.addActionListener (new ActionListener() {
    			public void actionPerformed (ActionEvent events)
    			{
    				goRoom("south");
    				if(currentRoom == room1){
    					pic.setIcon(roompic1);
    					list.setListData(listItems());
    				}else if(currentRoom == room2){
    					pic.setIcon(roompic2);
    					list.setListData(listItems());
    				}else if(currentRoom == room3){
    					pic.setIcon(roompic3);
    					list.setListData(listItems());
    				}else if(currentRoom == room4){
    					pic.setIcon(roompic4);
    					list.setListData(listItems());
    				}else{
    					JOptionPane.showMessageDialog(null, "Wha");
    				}
    			
    			}
    			
    			
    		}
    		);
    		
    		
    		JButton east = new JButton("East");
    		east.addActionListener (new ActionListener() {
    			public void actionPerformed (ActionEvent events)
    			{
    				goRoom("east");
    				 if(currentRoom == room1){
    					pic.setIcon(roompic1);
    					list.setListData(listItems());
    				}else if(currentRoom == room2){
    					pic.setIcon(roompic2);
    					list.setListData(listItems());
    				}else if(currentRoom == room3){
    					pic.setIcon(roompic3);
    					list.setListData(listItems());
    				}else if(currentRoom == room4){
    					pic.setIcon(roompic4);
    					list.setListData(listItems());
    				}else{
    					JOptionPane.showMessageDialog(null, "Wha");
    				}
    			
    			}
    		}
    		);
    		
    		
    		JButton west = new JButton("West");
    		west.addActionListener (new ActionListener() {
    			public void actionPerformed (ActionEvent events)
    			{
    				goRoom("west");
    				if(currentRoom == room1){
    					pic.setIcon(roompic1);
    					list.setListData(listItems());
    				}else if(currentRoom == room2){
    					pic.setIcon(roompic2);
    					list.setListData(listItems());
    				}else if(currentRoom == room3){
    					pic.setIcon(roompic3);
    					list.setListData(listItems());
    				}else if(currentRoom == room4){
    					pic.setIcon(roompic4);
    					list.setListData(listItems());
    				}else{
    					JOptionPane.showMessageDialog(null, "Wha");
    				}
    			
    			}
    			
    		}
    		
    		);
    		
    		
    		JButton back = new JButton ("Back");
    		back.addActionListener (new ActionListener() {
    			public void actionPerformed (ActionEvent events)
    			{
    				currentRoom = previousRoom;
    				if(currentRoom == room1){
    					pic.setIcon(roompic1);
    					list.setListData(listItems());
    				}else if(currentRoom == room2){
    					pic.setIcon(roompic2);
    					list.setListData(listItems());
    				}else if(currentRoom == room3){
    					pic.setIcon(roompic3);
    					list.setListData(listItems());
    				}else if(currentRoom == room4){
    					pic.setIcon(roompic4);
    					list.setListData(listItems());
    				}else{
    					JOptionPane.showMessageDialog(null, "Wha");
    				}
    			}
    		}
    		);
    		
    		JButton pickup = new JButton("Pick Up");
    		pickup.addActionListener (new ActionListener() {
    			public void actionPerformed (ActionEvent events)
    			{
    				//JOptionPane.showMessageDialog(null, pickitem);
    				getItem(pickitem);
    				
    				list.setListData(listItems());
    				
    			}
    		}
    	    ); 	
    		    
    	    
    		JButton drop = new JButton("Drop");
    		JButton dropAll = new JButton ("Drop all");		
    	
    		JLabel label2 = new JLabel("         ");
    		JList list2 = new JList(items);
    		JLabel roomitems = new JLabel("Items in room:");
    		JLabel handitems = new JLabel("Items in hand:");
    		
    		gbCons.fill = GridBagConstraints.BOTH;
    		
    		gbCons.weightx=0;
    		gbCons.weighty=0;
    		addComponent(north, 0, 1, 1, 1);
    		addComponent(south, 2, 1, 1, 1);
    		addComponent(back, 1, 1, 1, 1);
    		addComponent(east, 1, 2, 1, 1);
    		addComponent(west, 1, 0, 1, 1);
    		addComponent(label2, 3, 0, 1, 1);	
    		addComponent(pickup, 4, 0, 3, 1);
    		addComponent(drop, 5, 0, 3, 1);
    		addComponent(dropAll, 6, 0, 3, 1);
    		addComponent(roomitems, 7,0,3,1);
    		addComponent(handitems,9,0,3,1);
    		
    		gbCons.weightx=1000;
    		gbCons.weighty=1;
    		
    		addComponent(list, 8, 0, 3, 1);
    		addComponent(list2, 10, 0, 3, 1);
    		addComponent(pic, 0, 5, 0, 0);
    		
    		setJMenuBar(bar);
    		setSize(800,500);
    		setVisible(true);
    		setResizable(false);
    		
     307   }
    


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    Where is items getting populated?

    You set i from list, but then use it to read from items, and I don't see where you populate it (unless in setItems() which isn't listed).

    The error suggests that item doesn't contain an ith element...which suggests that the contents of list and item are not the same.

    Alternately, if they have the same contents, is it possible that nothing is selected in the list, giving you a value for i of -1, which won't exist in a 0-based array?


  • Advertisement
  • Closed Accounts Posts: 49 boyracer87


    Bonkey man...your a saint, a god damb saint....iv been looking at that code for the full day trying to figure it out...takes some fresh eyes i suppose.... "items" is actualy an empty vector where i was ment to put items after i took them out of a room...i should in fact of had "roomitems" in there instead...


    Cheers guys,
    Ray


  • Moderators, Science, Health & Environment Moderators Posts: 10,079 Mod ✭✭✭✭marco_polo


    boyracer87 wrote:
    Bonkey man...your a saint, a god damb saint....iv been looking at that code for the full day trying to figure it out...takes some fresh eyes i suppose.... "items" is actualy an empty vector where i was ment to put items after i took them out of a room...i should in fact of had "roomitems" in there instead...


    Cheers guys,
    Ray

    That was what I suspected alright, I see everything is under control here now. :cool:


Advertisement