Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 二维数组的奇怪错误_Java_Arrays_Multidimensional Array_2d - Fatal编程技术网

Java 二维数组的奇怪错误

Java 二维数组的奇怪错误,java,arrays,multidimensional-array,2d,Java,Arrays,Multidimensional Array,2d,我是java的初学者,我遇到了一个我认为非常奇怪的错误。以下是我运行程序时出现的错误: java.lang.ExceptionInInitializerError Caused by: java.lang.ArrayIndexOutOfBoundsException: 0 at test.mob.<init>(mob.java:14) at test.test.<clinit>(test.java:21) Exception in thread "main" Java R

我是java的初学者,我遇到了一个我认为非常奇怪的错误。以下是我运行程序时出现的错误:

java.lang.ExceptionInInitializerError
Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
at test.mob.<init>(mob.java:14)
at test.test.<clinit>(test.java:21)
Exception in thread "main" Java Result: 1
java.lang.ExceptionInInitializeError
原因:java.lang.ArrayIndexOutOfBoundsException:0
在test.mob.(mob.java:14)
test.test.(test.java:21)
线程“main”Java结果中出现异常:1
我在NetBeans中编程,但错误不会显示在IDE中,它只在我运行程序时显示。这是我在mob类中的代码,看看你是否能找到问题

package test;

public class mob {
    int counter = 0;
    int[][] mob;
    int loopCount = 0;
    int loopCount2 = 0;

    public mob(){
    //0: x pos
    //1: y pos

     mob = new int[counter][1];
     mob[counter][0]=test.width;         
     mob[counter][1]=test.height/2;
     counter++;
}
public void mobLoop(){
    while(loopCount <=counter){
        while(loopCount2<2){
        mob[loopCount][0]--;
        loopCount2++;
        }
        loopCount2 = 0;
        loopCount++;
    }
    return;
    }

}
封装测试;
公营暴徒{
int计数器=0;
int[][]移动;
int loopCount=0;
int loopCount2=0;
公众暴民(){
//0:x位置
//1:y位置
mob=新整数[计数器][1];
移动[计数器][0]=测试宽度;
移动[计数器][1]=测试高度/2;
计数器++;
}
公共void mobLoop(){

while(loopCount数组在Java中是从零开始索引的。您正在创建一个大小为[0][1]的数组,它在第一个维度中有零个元素,在第二个维度中有一个元素。因此,当您尝试访问此行中的数组时:

mob[counter][1]=test.height/2;

您在两个维度上都超出了界限。您需要在两个维度上都添加1,以根据我看到的代码保留界限。

在Java中数组从零开始索引。您正在创建一个大小为[0][1]的数组,它在第一个维度中有零个元素,在第二个维度中有一个元素。因此,当您尝试访问此行上的数组时:

mob[counter][1]=test.height/2;

您在这两个维度上都超出了界限。您需要在这两个维度上添加1,以保留基于我看到的代码的界限。

为了将来参考,您收到的错误是
运行时错误
。它们不会在IDE中显示为错误,称为
编译错误
。您将看到的唯一通知类型是关于IDE中的运行时错误将是一个警告。为了将来参考,您收到的错误是一个
运行时错误
。它们不会在IDE中显示为错误,这些错误称为
编译错误
。关于IDE中的运行时错误,您将收到的唯一通知是警告。