线程“AWT-EventQueue-0”中的异常 java.lang.ArrayIndexOutOfBoundsException: 1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12618564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:34:58  来源:igfitidea点击:

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

javaarraysswingjtableindexoutofboundsexception

提问by user1702856

I've been having a hard time trying to debug this exception: Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1

我一直在努力调试这个异常: Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1

This is my code

这是我的代码

package com.Homework.Table;

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class ArrayTable  {
    static String [][] records;

    public static void main(String [] args)
    {
        String [] columns= { "col1", "col2", "col3","col4" };
        String array ="A&B&1&May 8 2011 12:17AM;;E&D&5&May 8 2011 12:43AM;;F&G&5&May 8 2011 7:06AM;;H&I&1&May 14 2011 11:57PM";
        records=to2dim (array ,";;","&");
        Object rows[][] =records;
        JFrame f = new JFrame("JTable Sample");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container content = f.getContentPane();
        JTable table = new JTable(rows, columns);
        JScrollPane scrollPane = new JScrollPane(table);
        content.add(scrollPane, BorderLayout.CENTER);
        f.setSize(300, 200);
        f.setVisible(true);
    }

    public static String [][] to2dim (String source , String outerdelim, String innerdelim) {
        String [][] result = new String [source.replaceAll ("[^" + outerdelim + "]", "").length () + 1][]; 
        int count = 0;
        for (String line : source.split ("[" + outerdelim + "]"))
        {
            result [count++] = line.split (innerdelim);
        }
        return result;
    }
}

This is the response that I get

这是我得到的回应

 Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
at javax.swing.JTable.getValueAt(Unknown Source)
at javax.swing.JTable.getValueAt(Unknown Source)
at javax.swing.JTable.prepareRenderer(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JViewport.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(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)

Help me.

帮我。

回答by sakthisundar

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.util.Arrays;
public class ArrayTable  {
    static String [][] records;

    public static void main(String [] args)
    {
        String [] columns= { "col1", "col2", "col3","col4" };
        String array ="A&B&1&May 8 2011 12:17AM;;E&D&5&May 8 2011 12:43AM;;F&G&5&May 8 2011 7:06AM;;H&I&1&May 14 2011 11:57PM";
        records=to2dim (array ,";;","&");
        Object rows[][] =records;
        JFrame f = new JFrame("JTable Sample");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container content = f.getContentPane();
        JTable table = new JTable(rows, columns);
        JScrollPane scrollPane = new JScrollPane(table);
        content.add(scrollPane, BorderLayout.CENTER);
        f.setSize(300, 200);
        f.setVisible(true);
    }

    public static String [][] to2dim (String source , String outerdelim, String innerdelim) {

        String [][] result = new String [(source.split (outerdelim)).length][];
        int count = 0;

        System.out.println("result len "+result.length);
        for (String line : source.split (outerdelim ))
            {
                result [count++] = line.split (innerdelim);
            }

        System.out.println(Arrays.deepToString(result));
        return result;
    }
}

This should do for you. [;;]matches only single character ,hence split based on this will add space in between two consecutive ;. So it should be changed to just ;;.

这应该对你有用。[;;]仅匹配单个字符,因此基于此拆分将在两个连续字符之间添加空格;。所以应该改为 just ;;

回答by Mikhail Selivanov

You have only one column in 2-nd, 4-th and 6-th rows. Should be 4 columns.

您在第 2、第 4 和第 6 行中只有一列。应该是4列。