0

Hi there, I am having a problem in running my frame at Java Eclipse. It always says unkown source when clicking the button. I would like to insert the dat to the Jtable but it always display like this

`

This is the error:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
    at myGUI.Summary.actionPerformed(Summary.java:67)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown 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.mouseReleased(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.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(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)






package myGUI;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class Summary extends JFrame implements ActionListener{
    /**
     * 
     */
//  private static final long serialVersionUID = 1L;
    private JTable table;
    private JButton btnInput;
    private JTextField txtField;


     private String[] row={"FirstName", "MiddleName","LastName"};
    private String[][] data ;
    //public Vector fName,mname,lname;
    private int x;
    public Summary(){
        x=0;
        txtField=new JTextField("Type here",10);
        data=new String[x][3];
        btnInput=new JButton("in");
        btnInput.addActionListener(this);
        table=new JTable(data,row);
        table.getColumnModel().getColumn(0).setPreferredWidth(10);
        table.getColumnModel().getColumn(1).setPreferredWidth(100);
    table.getColumnModel().getColumn(2).setPreferredWidth(150);

        JScrollPane scrollPane = new JScrollPane(table);
        JPanel panelDown =new JPanel();
        setLayout(new GridLayout());
        panelDown.add(scrollPane);
        panelDown.add(txtField);
        panelDown.add(btnInput);
        add(panelDown);

        //  add(panelDown);



        setVisible(true);
        setSize(500,500);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }
    public static void main(String[] args){
        //Summary s=new Summary();
        //input in=new input();
        new Summary();

    }
    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        if(arg0.getSource().equals(btnInput)){
            //Summary sum=new Summary();
            String disp=txtField.getText();

        data[x][0]=disp;
        data[x][1]=disp;
        data[x][2]=disp;
        x++;

        }


    }

}

Edited by Jeanyl_1

2
Contributors
1
Reply
9
Views
2 Years
Discussion Span
Last Post by JamesCherrill
1

The "unknown source" is just referring to the internals of the Java API classes (for which the source code is not on your computer).
The first lines of the message tell you what you need to know...

ArrayIndexOutOfBoundsException: 0 at myGUI.Summary.actionPerformed(Summary.java:67)

that looks like line 111 in the code as you posted it (?), if so it looks like all or part of the data[][] array is uninitialised - ie does not have a member [0]

looking at lines 72-74 it seems you have initialised data to size [0][3], so there's your problem

Votes + Comments
Instructive & helpful
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.