初始化objectts的2维数组时ArrayIndexOutOfBoundsException异常 [英] ArrayIndexOutOfBoundsException when initializing a 2dimensional array of objectts

查看:153
本文介绍了初始化objectts的2维数组时ArrayIndexOutOfBoundsException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的问题,但为什么我有这个例外,我想不通。我试图创建对象的一个​​数独题的二维阵列,但是当我初始化我得到 ArrayIndexOutOfBoundsException异常。请帮帮忙,我读过类似的问题,这应该是工作!

下面我宣布格(使用和构造对象的2维数组):

 公共类数独扩展javax.swing.JFrame中{
        私人诠释线;
        细胞[] []网格;    公共数独(){
    的initComponents();
    格=新的细胞[线] [线];

所以,当我cliking一个按钮设置线(尺寸长度)如下图所示。

 私人无效jButton1ActionPerformed(EVT java.awt.event.ActionEvent中){
    线= 10;
    makeGrid(线);
}

我收到了异常:

 异常螺纹AWT-EventQueue的-0java.lang.ArrayIndexOutOfBoundsException:0
在Sudoku.makeGrid(Sudoku.java:146)    公共无效makeGrid(INT大小){          的for(int i = 0; I<大小;我++)
                对于(INT J = 0; J<大小; J ++){
    146:电网[I] [J] =新的细胞();
                }
          }


解决方案

您应该将你的网格初始化到结构网格方法,因为在构造函数中成员仍不是你的所需值初始化(INT的默认值是0,所以你得到一个空数组并尝试用更大的未分配的界限之后访问)

 公共无效makeGrid(INT大小){
      this.lines =大小; //如果你不需要其他地方行则是多余的
      格=新电池[大小] [SIZE]
      的for(int i = 0; I<大小;我++)
            对于(INT J = 0; J<大小; J ++){
              电网[I] [J] =新的细胞();
            }
      }

I have a very simple question but i can't figure out why I'm having this exception. I'm trying to create a 2-dimensional Array of objects for a sudoku puzzle, but when I'm initializing i'm getting ArrayIndexOutOfBoundsException. Please help, I've read similar questions and it should be working!

Here I'm declaring the grid(2-dimensional array of objects used and constructor):

    public class Sudoku extends javax.swing.JFrame {
        private int lines;
        Cell[][] grid;

    public Sudoku() {
    initComponents();
    grid = new Cell[lines][lines];

So when i'm cliking a button to set the lines(size length) as shown below

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    lines=10;
    makeGrid(lines);
}

I'm getting the exception:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
at Sudoku.makeGrid(Sudoku.java:146)

    public void makeGrid(int size) {

          for(int i=0;i<size;i++)
                for(int j=0;j<size;j++)    {
    146:                grid[i][j] = new Cell();
                }
          }

解决方案

You should move your grid initialization into the make grid method since in the constructor the member lines is still not initialized with your desired value (default value of int is 0 so you get an empty array and you try to access it afterwards with bigger unallocated bounds)

public void makeGrid(int size) {
      this.lines = size; // If you do not need lines anywhere else then it is redundant
      grid = new Cell[size][size];
      for(int i=0;i<size;i++)
            for(int j=0;j<size;j++)    {
              grid[i][j] = new Cell();
            }
      }

这篇关于初始化objectts的2维数组时ArrayIndexOutOfBoundsException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆