Problem with arrays

java.lang.ArrayIndexOutOfBoundsException

Page 1 of 1

6 Replies - 1214 Views - Last Post: 07 December 2009 - 03:54 AM Rate Topic: -----

#1 d.steiner   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-December 09

Problem with arrays

Posted 06 December 2009 - 04:31 PM

Hi,

So I'm new to Java, and I chose to make a small program to calculate coordinates in a new basis as my first project. However, when i add events to basisButtons (an array of buttons), i keep getting an error of ArrayIndexOutOfBoundsException from indexButtons[], and i just dont get it why. Any help would be appreciated. The code is still far from ready, only looking for solutions for this problem.

package mathsswing;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class MainGUI extends JFrame {
		int i,j;
		int rownr;
		int colnr;
		JTextField matrixCells[][];
		
		private javax.swing.JMenuBar menuBar;
		private javax.swing.JMenuItem mexitItem;
		private javax.swing.JMenuItem mbaseItem;
		private javax.swing.JPanel mainPanel;
		private javax.swing.JMenu fileMenu;
		private javax.swing.JPanel myPanel[];
		private javax.swing.JButton basisButtons[][];
		JButton indexButtons[];
		String colindex[];
		String rowindex[];
		
	private void initComponents() {
		mainPanel = new javax.swing.JPanel();
		menuBar = new javax.swing.JMenuBar();
		mbaseItem = new javax.swing.JMenuItem("Basis transformation");
		mexitItem = new javax.swing.JMenuItem("Exit");
		fileMenu = new javax.swing.JMenu("File");
		myPanel = new JPanel[10];

		for(i=0;i<10;i++) {
			myPanel[i]= new JPanel();
			myPanel[i].setVisible(false);
			mainPanel.add(myPanel[i]);
		}
		
		fileMenu.add(mbaseItem);
		fileMenu.add(mexitItem);
		menuBar.add(fileMenu);
		mainPanel.setBounds(15,30,600,400);
		mexitItem.addActionListener(new ExitButtonListener());
		mbaseItem.addActionListener(new ActionListener() 
			{	public void actionPerformed(ActionEvent e) {
				myPanel[1].setVisible(true);
		}});
		
		setJMenuBar(menuBar);
		setContentPane(mainPanel);
		mainPanel.setVisible(true);
		
		int j;
		String numbers[] = new String[10];
		for(j=1; j<11; j++) { numbers[j-1] = Integer.toString(j); };
		final JComboBox rowsCBox = new JComboBox(numbers);
		final JComboBox columnsCBox = new JComboBox(numbers);
		JButton rowscolsOK = new JButton("OK");
		rowscolsOK.addActionListener(new ActionListener(){ 
			public void actionPerformed(ActionEvent e){
				rownr = Integer.parseInt((String) rowsCBox.getSelectedItem());
				colnr = Integer.parseInt((String) columnsCBox.getSelectedItem());
				myPanel[1].setVisible(false);
				matrixCells = new JTextField[rownr][colnr];
				myPanel[2].setLayout(new GridLayout(rownr+1,colnr));
				myPanel[2].add(new JLabel("Set values of the matrix"));
				JButton addData = new JButton("OK");
				addData.addActionListener(new CreateTableListener());
				for(i=2;i<colnr;i++) {myPanel[2].add(new JLabel(" "));}
				myPanel[2].add(addData);
				int k,l;
				for(k=0;k<rownr;k++) {
					for(l=0;l<colnr;l++) {
					matrixCells[k][l]= new JTextField();
					myPanel[2].add(matrixCells[k][l]);
					}
				}
				

				myPanel[2].setVisible(true);
			}
		});
		myPanel[1].setLayout(new GridLayout(3,2));
		myPanel[1].add(new JLabel("Number of Rows"));
		myPanel[1].add(rowsCBox);
		myPanel[1].add(new JLabel("Number of Columns"));
		myPanel[1].add(columnsCBox);
		myPanel[1].add(rowscolsOK);
	}
	
	public MainGUI() { 
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setTitle("Quantitative Economics");
		setSize(800,600);
		setResizable(true);
		
		initComponents();
		
	}
	class ExitButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
			
		}
	}
	class CreateTableListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			myPanel[2].setVisible(false);
			int matrixData[][] = new int[rownr][colnr];
			String matrixString[][] = new String[rownr][colnr];
			colindex= new String[colnr];
			rowindex= new String[rownr];
			for(i=0;i<rownr;i++){for(j=0;j<colnr;j++) { matrixData[i][j]=Integer.parseInt(matrixCells[i][j].getText()); matrixString[i][j]=Integer.toString(matrixData[i][j]);}}
			myPanel[3].setLayout(new GridLayout(rownr+1,colnr+1));
			myPanel[3].add(new JButton(" "));
			indexButtons = new JButton[colnr];
			for(i=0;i<colnr;i++){ colindex[i] = new String("x" + (i+1)); indexButtons[i]= new JButton(colindex[i]); myPanel[3].add(indexButtons[i]); }
			myPanel[3].setVisible(true);
			basisButtons = new JButton[rownr][colnr];
			for(i=0;i<rownr;i++) {
				rowindex[i] = new String("e" + (i+1));
				myPanel[3].add(new JButton(rowindex[i]));
				for(j=0;j<colnr;j++) {
					basisButtons[i][j] = new JButton(matrixString[i][j]);
					myPanel[3].add(basisButtons[i][j]);	
				}	
			} 
			for(i=0;i<colnr;i++) {
				for(j=0;j<rownr;j++) {
					basisButtons[j][i].addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							indexButtons[i].setText("FOR THIS IM GETTING THE ERROR");
						}});
					
				}
			}
			
		}
	}

	public static void main(String[] args) { 
		MainGUI mainwindow = new MainGUI();
		mainwindow.setVisible(true);
	}

}


I get "Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException", line 133.

Thanks in advance and sorry for my poor English, I'm not native.

David

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with arrays

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Problem with arrays

Posted 06 December 2009 - 08:38 PM

Obviously

		   for(i=0;i<colnr;i++) {
				for(j=0;j<rownr;j++) {
					basisButtons[j][i].addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							indexButtons[i].setText("FOR THIS IM GETTING THE ERROR");
						}});
					
				}
			}



indexButtons[] was not initialized to colnr length
Was This Post Helpful? 0
  • +
  • -

#3 d.steiner   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-December 09

Re: Problem with arrays

Posted 07 December 2009 - 02:19 AM

I thought it was initialized here:
  
indexButtons = new JButton[colnr];

			for(i=0;i<colnr;i++){ colindex[i] = new String("x" + (i+1)); indexButtons[i]= new JButton(colindex[i]); myPanel[3].add(indexButtons[i]); }



And I can any time use it outside the actionPerformed method. It already has a given value, that i want to change with the method.

This post has been edited by d.steiner: 07 December 2009 - 02:23 AM

Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Problem with arrays

Posted 07 December 2009 - 02:39 AM

You're miscounting your index buttons. Replace with this:

				System.out.printf("There are %d index buttons, indexed 0-%d\n", indexButtons.length, indexButtons.length - 1);
				indexButtons[i].setText("FOR THIS IM GETTING THE ERROR");


Was This Post Helpful? 0
  • +
  • -

#5 d.steiner   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-December 09

Re: Problem with arrays

Posted 07 December 2009 - 03:13 AM

Thanks alot, that actually helped me, but i still don't really understand why..

							System.out.printf("There are %d index buttons, indexed 0-%d\n", indexButtons.length, indexButtons.length - 1);
							indexButtons[i-1].setText("FOR THIS IM GETTING THE ERROR");



I don't get the exception any more, however it doesn't work as I think it should. Anyways... How come indexButtons[i] is out of bounds, when I priorly initialize it with a for loop (i=0, i<colnr, i++), and i have basicly the same loop here. If I set colnr to =4, i get 4 indexbuttons, indexed 0-3. And my for loop is also going from 0 to 3 here. Sorry if its obvious, I'm really new to this stuff and I'm a bit confused about this.

This post has been edited by d.steiner: 07 December 2009 - 03:15 AM

Was This Post Helpful? 0
  • +
  • -

#6 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Problem with arrays

Posted 07 December 2009 - 03:30 AM

I should have made it more explicit. Use this instead:

				System.out.printf("There are %d index buttons, indexed 0-%d, which you're addressing by index %d\n", indexButtons.length, indexButtons.length - 1, i);


Was This Post Helpful? 1
  • +
  • -

#7 d.steiner   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-December 09

Re: Problem with arrays

Posted 07 December 2009 - 03:54 AM

Alrite, thank you very much, I'll rethink it with your provided help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1