简体   繁体   English

为什么我得到java.lang.ArrayIndexOutOfBoundsException

[英]Why am I getting java.lang.ArrayIndexOutOfBoundsException

So basically, I'm trying to make a grid of 144 buttons (12x12) by making 12 rows down with 12 buttons along each row. 所以基本上,我试图通过向下制作12行,每行12个按钮,制作一个144个按钮(12x12)的网格。

However, when I try to run, I get this error: 但是,当我尝试运行时,出现此错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at progrid.me.myname.grid.ProGrid.main(ProGrid.java:23)

Here's the code: 这是代码:

package progrid.me.myname.grid;

import javax.swing.*;

public class ProGrid extends JFrame {   
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public static void main(String[] arguments) {
        JFrame frame = new JFrame(); //Create the frame
        frame.pack();
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("My ProGrid remake");
        frame.setSize(300, 300);

        JPanel[] row = new JPanel[12];
    for(int i = 0; i < 12; i++){
        frame.add(row[i]);
            row[i].setBounds(0, i*25, 300, 25);
            row[i].setVisible(true);


            for(int j = 0; i < 12; i++){
                JButton[] button = new JButton[j]; //Make 12 new JButtons (button[0] - button[12]) for each row
                row[i].add(button[j]);
                button[j].setBounds(i*25, 0, 25, 25);
                button[j].setVisible(true);


            }
        }

    }

}

Line 23 is this: 第23行是这样的:

frame.add(row[i]);

Which probably means this won't work either: 这可能意味着这也不起作用:

row[i].add(button[j]);

So could anyone give me a little shove in the right direction? 那么,有人能给我一点正确的方向吗? I'm not asking for you to dump code at me, I'm actually trying to learn. 我不是要您向我转储代码,而是在尝试学习。 But some help would be great. 但是一些帮助会很棒。 Thanks! 谢谢!

EDIT Fixed the first problem, now I'm getting java.lang.NullPointerException on line frame.add(row[i]); 编辑修复了第一个问题,现在我在frame.add(row [i]);行上获取了java.lang.NullPointerException

problem: 问题:

JPanel[] row = new JPanel[i];

You are creating a new array of JPanel and JButton in each iteration of your loop, which results in an ArrayIndexOutOfBoundsException on the first iteration of your loop for i=0 when it executes JPanel[] row = new JPanel[0]; frames.add(row[0]); 您将在循环的每次迭代中创建一个新的JPanelJButton数组,这将在您执行JPanel[] row = new JPanel[0]; frames.add(row[0]);时在i=0循环的第一次迭代中导致ArrayIndexOutOfBoundsException ,它执行JPanel[] row = new JPanel[0]; frames.add(row[0]); JPanel[] row = new JPanel[0]; frames.add(row[0]);

solution: 解:

For both arrays, create only one instance of the array outside the loop, and update that instance in each iteration of the loop. 对于这两个数组,仅在循环外部创建该数组的一个实例,并在循环的每次迭代中更新该实例。 Here's an example for the JPanel array: 这是JPanel数组的示例:

JPanel[] row = new JPanel[12]; //Make 12 new JPanels (row[0] - row[12])
for(int i = 0; i < row.length; i++){
        frame.add(row[i]);
        row[i].setBounds(0, i*25, 300, 25);
        row[i].setVisible(true);

EDIT: 编辑:

JPanel[] row = new JPanel[12];
    for (int i = 0; i < 12; i++) {
        row[i] = new JPanel();
        frame.add(row[i]);
        row[i].setBounds(0, i * 25, 300, 25);
        row[i].setVisible(true);

        JButton[] button = new JButton[12];
        for (int j = 0; i < 12; i++) {
            button[j] = new JButton();
            row[i].add(button[j]);
            button[j].setBounds(i * 25, 0, 25, 25);
            button[j].setVisible(true);

        }
    }

Row[0] to row[12] will create 13 rows. 第[0]行至第[12]行将创建13行。 Make it row[11] 使其排[11]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么会出现java.lang.ArrayIndexOutOfBoundsException异常? - Why am I getting java.lang.ArrayIndexOutOfBoundsException exception ? java.lang.ArrayIndexOutOfBoundsException 为什么我得到这个 - java.lang.ArrayIndexOutOfBoundsException why am i getting this 获取java.lang.ArrayIndexOutOfBoundsException:2&gt; = 2 - Getting java.lang.ArrayIndexOutOfBoundsException: 2 >= 2 我一直在获取java.lang.ArrayIndexOutOfBoundsException:500 - I keep getting java.lang.ArrayIndexOutOfBoundsException: 500 我不断收到 java.lang.ArrayIndexOutOfBoundsException 但找不到错误 - I keep getting a java.lang.ArrayIndexOutOfBoundsException but cannot find the error 为什么我要从SAP HANA JDBC驱动程序中获取java.lang.ArrayIndexOutOfBoundsException? - Why would I be getting an java.lang.ArrayIndexOutOfBoundsException from SAP HANA JDBC Driver? 为什么会收到“ java.lang.ArrayIndexOutOfBoundsException:0” - Why do I get an “java.lang.ArrayIndexOutOfBoundsException: 0” 为什么我在这里得到 java.lang.ArrayIndexOutOfBoundsException ? - Why do I get java.lang.ArrayIndexOutOfBoundsException here? 如何避免出现java.lang.ArrayIndexOutOfBoundsException - How to avoid getting java.lang.ArrayIndexOutOfBoundsException 在groovy中获取java.lang.ArrayIndexOutOfBoundsException错误 - getting java.lang.ArrayIndexOutOfBoundsException error in groovy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM