0

I have an error when I run my code, can you help me to solve the problem?

When I write 1 in kindnummer JTextField, that works but when I change the value to 2 or 3 , I show the Error:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 2

My TableModel Class:

public class listtable extends AbstractTableModel{
       BusinessDelegate bd= new BusinessDelegate();
       JTextField t= new JTextField();

        private final String[] entetes = { "Name", "Vorname", "Geburtsdatum", "Geschlecht" };
       String rr=t.getText();
        private    List<Kiwikinder> cc =  bd.getRemoteEjb().getKiwidinderbyNummerFindbyId(1);
    public listtable(List<Kiwikinder> cc) {
              this.cc = new ArrayList<>(cc);
          }

      public Class getColumnClass(int c) {
          return getValueAt(0, c).getClass();
      } 
    @Override
    public int getColumnCount() {

        // TODO Implement this method
        return entetes.length;
    }

    @Override
    public String getColumnName(int columnIndex) {
        // TODO Implement this method
        return entetes [columnIndex];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Kiwikinder kk= cc.get(rowIndex);
        Object value = null;
        switch(columnIndex) {
        case 0:
            value= kk.getName();
            break;
        case 1:
        value=kk.getVorname();
        break;
        case 2:
        value= kk.getGeburtsdatum();
        break;
        case 3:
            value= kk.getGeschlecht();
            break;

        }
              return  value;
          }

    @Override
    public boolean isCellEditable(int i, int i2) {
        // TODO Implement this method
        return true;
    }

    @Override
    public void setValueAt(Object object, int i, int i2) {
       Kiwikinder hh=cc.get(i);
            if (i2==0)
           hh.setName((String)object);
            else             if (i2==1)
           hh.setVorname((String)object);
            else    if (i2==2)
                hh.setGeburtsdatum((Date)object);
                if (i2==3)
                hh.setGeschlecht((String)object);
            fireTableCellUpdated(i,i2);
    }

    @Override
    public int getRowCount() {
         return cc.size();
    }
    public void updateRow(int index,String[] values)
            {
                for (int i = 0 ; i < values.length ; i++)
                {
                    setValueAt(values[i],index,i);
                }
            }
}

And my 2nd Class:

public class tabbedPane extends JFrame implements ActionListener {
    BusinessDelegate businessDelegate = new BusinessDelegate();
    private JTabbedPane tabbedPane;
    private JPanel panel1, panel2, panel3; 
    private JButton kindsuchen;
    private JTextField   kindernummer;
    Kiwiantraagsteller ant = new Kiwiantraagsteller();
    InputVerifier verifier = new Verifier();
    private listtable model;
   private JTable table;     
    public tabbedPane() {

        setTitle("Kindergeld");
        setSize(700, 800);
        setBackground(Color.gray);   
        kindsuchen = new JButton("suchen");         
       JPanel topPanel = new JPanel();
        topPanel.setLayout(new BorderLayout());
        getContentPane().add(topPanel);

        // Create the tab pages
        createPage1();
        createPage2();
        createPage3();


        // Create a tabbed pane
        tabbedPane = new JTabbedPane();
        tabbedPane.addTab("Neuer Antragsteller stellen", panel1);
        tabbedPane.addTab("Neues Kind stellen ", panel2);
        tabbedPane.addTab("Suchen Kindergeldfall", panel3);
        topPanel.add(tabbedPane, BorderLayout.CENTER);

    }

    public void createPage1() {
         panel1 = new JPanel()
        panel1.setLayout(null);

    }

    public void createPage2() {
         panel2 = new JPanel();
        panel2.setLayout(null);

    }

    public void createPage3() {
        kindernummer = new JTextField();
        panel3 = new JPanel();
        panel3.setLayout(null);
        JLabel label12 = new JLabel("Kindergeldnummer:");
        label12.setBounds(10, 20, 150, 20);
        panel3.add(label12);
    table= new JTable();
        kindernummer.setBounds(150, 20, 200, 20);
        kindsuchen.addActionListener(this);
        panel3.add(kindernummer);
        kindsuchen.setBounds(350, 40, 90, 25);
        panel3.add(kindsuchen);
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        BusinessDelegate businessDelegate = new BusinessDelegate();      
        if (actionEvent.getSource() == kindsuchen) {

            String tr = kindernummer.getText();
         Integer  g = Integer.valueOf(tr);
           List <Kiwikinder> cc = businessDelegate.getRemoteEjb().getKiwidinderbyNummerFindbyId(g);

         model = new listtable(cc);               table = new JTable(model);
           table.setModel(model);
            table.setBounds(10, 50, 200, 20);
            JScrollPane tableContainer = new JScrollPane(table);
            tableContainer.setBounds(10, 80, 600, 150);
            panel3.add(tableContainer, BorderLayout.CENTER);
           table.setAutoCreateRowSorter(true);
          }

        table.getModel().addTableModelListener(new TableModelListener() {
                    @Override
                    public void tableChanged(TableModelEvent e) {
                    }
               });    
        }

    public static void main(String args[]) {
        // Create an instance of the test application
        tabbedPane bb = new tabbedPane();
        bb.setVisible(true);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Doni
  • 23
  • 3
  • 5
  • Please be more specific and post only relevant parts. – Maroun Jan 14 '15 at 10:24
  • I would like to output data from my Database and show them in JTable , i have used a Table Model , i can only search my data im server and show there haven a Kindnummer equal to 1,but i would like to show alle die Data mit different Kindnummer parameter, wehn i click 2 for example in kindnummer(JTextField) i show the data relativ to the nummer but i can nicht – Doni Jan 14 '15 at 10:28
  • Post the full exception stack trace – BarrySW19 Jan 14 '15 at 10:38
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) – Andrew Thompson Jan 14 '15 at 10:45
  • the Exception:Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 2 at java.util.Vector.get(Vector.java:744) at kiwi.tabbedPane.actionPerformed(tabbedPane.java:524) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) atjavax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) atjava.awt.Component.processMouseEvent(Component.java:6505) – Doni Jan 14 '15 at 11:03

1 Answers1

0

The problem is, your array is too small the Program tries to write in a not existing place in your String Array. You have to opportunities to solve your Problem:

1.) Choose a bigger String Array.

2.) Choose an ArrayList instead of a String Array. You can initialize an ArrayList like this:

ArrayList<String> yourArrayList = new ArrayList<String>();
Pascal
  • 1,255
  • 5
  • 20
  • 44