We help IT Professionals succeed at work.

Java: ArrayIndexOutOfBoundsException: why?

epifanio67
epifanio67 asked
on
Hello Experts,

why am I getting an ArrayIndexOutOfBoundsException on the code below?

thanks for your help...

regards,
package com.tables2.ui;

import java.awt.BorderLayout;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class TableDemo extends JTable {

	private static final long serialVersionUID = 1L;
	String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" };
	Object[][] data = {
			{ "Kathy", "Smith", "Snowboarding", new Integer(5), new Boolean(false) },
			{ "John", "Doe", "Rowing", new Integer(3), new Boolean(true) },
			{ "Sue", "Black", "Knitting", new Integer(2), new Boolean(false) },
			{ "Jane", "White", "Speed reading", new Integer(20),new Boolean(true) },
			{ "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } };
	private JTable table = new JTable(data, columnNames);	

	private TableDemo() {
		getTableInstance();
	}

	public JTable getTableInstance() {
		return table;
	}
	
	public int getColumnCount(){
		return columnNames.length;
	}
	
	public int getRowCount(){
		return data.length;
	}
	
	public String getColumnName(int col){
		return columnNames[col];
	}
	
	public static void createAndShowGUI(){
		//Create and set up the window.
        JFrame frame = new JFrame("TableDemo");

        
        TableDemo td = new TableDemo();
        JScrollPane scrollPane = new JScrollPane(td);
        td.setFillsViewportHeight(true);
        
        JLabel lblHeading = new JLabel("test TableDemo Class");
        lblHeading.setFont(new Font("Arial",Font.TRUETYPE_FONT,24));

        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(lblHeading,BorderLayout.PAGE_START);
        frame.getContentPane().add(scrollPane,BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(550, 200);
        frame.setVisible(true);
        
 
	}
	
	public static void main(String[] args) {
		createAndShowGUI();
	}

}// END OF CLASS

Open in new window

Comment
Watch Question

Top Expert 2016
Commented:
Because you've not subclassed JTable correctly. Unless you've a very good reason for subclassing JTable and you really know what you're doing - never do so. Just use a good TableModel (normally DefaultTableModel) with a normal JTable. Kludgy fix below:


import java.awt.BorderLayout;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

//public class TableDemo extends JTable {
public class TableDemo {

	private static final long serialVersionUID = 1L;
	String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" };
	Object[][] data = {
			{ "Kathy", "Smith", "Snowboarding", new Integer(5), new Boolean(false) },
			{ "John", "Doe", "Rowing", new Integer(3), new Boolean(true) },
			{ "Sue", "Black", "Knitting", new Integer(2), new Boolean(false) },
			{ "Jane", "White", "Speed reading", new Integer(20),new Boolean(true) },
			{ "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } };
	//private JTable table = new JTable(data, columnNames);	
	private JTable table = new JTable(new DefaultTableModel(data, columnNames));	

	private TableDemo() {
		getTableInstance();
	}

	public JTable getTableInstance() {
		return table;
	}
	
	public int getColumnCount(){
		return columnNames.length;
	}
	
	public int getRowCount(){
		return data.length;
	}
	
	public String getColumnName(int col){
		return columnNames[col];
	}
	
	public static void createAndShowGUI(){
		//Create and set up the window.
        JFrame frame = new JFrame("TableDemo");

        
        TableDemo td = new TableDemo();
        //JScrollPane scrollPane = new JScrollPane(td);
        JScrollPane scrollPane = new JScrollPane(td.table);
        //td.setFillsViewportHeight(true);
        
        JLabel lblHeading = new JLabel("test TableDemo Class");
        lblHeading.setFont(new Font("Arial",Font.TRUETYPE_FONT,24));

        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(lblHeading,BorderLayout.PAGE_START);
        frame.getContentPane().add(scrollPane,BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(550, 200);
        frame.setVisible(true);
        
 
	}
	
	public static void main(String[] args) {
		createAndShowGUI();
	}

}// END OF CLASS

Open in new window

Awarded 2011
Awarded 2011
Commented:
This code does not give array out of bounds

//package com.tables2.ui;

import java.awt.*;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class TableDemo extends JTable {

	private static final long serialVersionUID = 1L;
	String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" };
	Object[][] data = {
			{ "Kathy", "Smith", "Snowboarding", new Integer(5), new Boolean(false) },
			{ "John", "Doe", "Rowing", new Integer(3), new Boolean(true) },
			{ "Sue", "Black", "Knitting", new Integer(2), new Boolean(false) },
			{ "Jane", "White", "Speed reading", new Integer(20),new Boolean(true) },
			{ "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } };
	//private JTable table = new JTable(data, columnNames);
    private JTable table = new JTable(new DefaultTableModel(data, columnNames));



	private TableDemo() {
        super();
        this.setModel(new DefaultTableModel(data, columnNames));
		//getTableInstance();
	}

	public JTable getTableInstance() {
		return table;
	}

	public int getColumnCount(){
		return columnNames.length;
	}

	public int getRowCount(){
		return data.length;
	}

	public String getColumnName(int col){
		return columnNames[col];
	}

	public static void createAndShowGUI(){
		//Create and set up the window.
        JFrame frame = new JFrame("TableDemo");


        TableDemo td = new TableDemo();
        JScrollPane scrollPane = new JScrollPane(td);
       // td.setFillsViewportHeight(true);

        JLabel lblHeading = new JLabel("test TableDemo Class");
        lblHeading.setFont(new Font("Arial",Font.TRUETYPE_FONT,24));

      
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(lblHeading,BorderLayout.PAGE_START);
        frame.getContentPane().add(scrollPane,BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(550, 200);


        frame.setVisible(true);


	}

	public static void main(String[] args) {
		createAndShowGUI();
	}

}// END OF CLASS

Open in new window

Awarded 2011
Awarded 2011

Commented:
This will have your getInstance method also

package com.tables2.ui;

import java.awt.*;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class TableDemo extends JTable {

	private static final long serialVersionUID = 1L;
	String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" };
	Object[][] data = {
			{ "Kathy", "Smith", "Snowboarding", new Integer(5), new Boolean(false) },
			{ "John", "Doe", "Rowing", new Integer(3), new Boolean(true) },
			{ "Sue", "Black", "Knitting", new Integer(2), new Boolean(false) },
			{ "Jane", "White", "Speed reading", new Integer(20),new Boolean(true) },
			{ "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } };
	//private JTable table = new JTable(data, columnNames);
  //  private JTable table = new JTable(new DefaultTableModel(data, columnNames));



	private TableDemo() {
        super();
        this.setModel(new DefaultTableModel(data, columnNames));
		//getTableInstance();
	}

	public JTable getTableInstance() {
		return new TableDemo();
	}

	public int getColumnCount(){
		return columnNames.length;
	}

	public int getRowCount(){
		return data.length;
	}

	public String getColumnName(int col){
		return columnNames[col];
	}

	public static void createAndShowGUI(){
		//Create and set up the window.
        JFrame frame = new JFrame("TableDemo");


        TableDemo td = new TableDemo();
        JScrollPane scrollPane = new JScrollPane(td);
       // td.setFillsViewportHeight(true);

        JLabel lblHeading = new JLabel("test TableDemo Class");
        lblHeading.setFont(new Font("Arial",Font.TRUETYPE_FONT,24));

      
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(lblHeading,BorderLayout.PAGE_START);
        frame.getContentPane().add(scrollPane,BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(550, 200);


        frame.setVisible(true);


	}

	public static void main(String[] args) {
		createAndShowGUI();
	}

}// END OF CLASS

Open in new window

Author

Commented:
I see the error... thank you experts...
Awarded 2011
Awarded 2011

Commented:
epifanio67,

Did you try to run the code you accepted as a solution?
Awarded 2011
Awarded 2011

Commented:
No, sorry, it really runs, I saw only one change, and didn't tsee the other
Top Expert 2016

Commented:
:)

Pay attention to my last sentence, btw - i'm not saying that's the way to code it - i DESCRIBED that