Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:0 at Point.main(Point.java:15)public class points {protected int x,y;public points(){setPoint(0,0);}public points(int a,int b){setPoint(a,b);}public void setPoint(int a ,int b){x=a;y

来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/20 01:07:08
Exceptioninthread"main"java.lang.ArrayIndexOutOfBoundsException:0atPoint.main(Point.java:15)publiccl

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:0 at Point.main(Point.java:15)public class points {protected int x,y;public points(){setPoint(0,0);}public points(int a,int b){setPoint(a,b);}public void setPoint(int a ,int b){x=a;y
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:0 at Point.main(Point.java:15)
public class points {
protected int x,y;
public points(){setPoint(0,0);}
public points(int a,int b){setPoint(a,b);}
public void setPoint(int a ,int b){
x=a;
y=b;
}
public int getX(){return x;}
public int getY(){return y;}
public String toString()
{return "[" +x+","+y+"]";}
}
但是运行的时候不对……就是一行上面的字

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:0 at Point.main(Point.java:15)public class points {protected int x,y;public points(){setPoint(0,0);}public points(int a,int b){setPoint(a,b);}public void setPoint(int a ,int b){x=a;y
这个异常是初学者比较常见的异常.
ArrayIndexOutOfBoundsException:注意这个单词,字面意思就是数组引用超出界限,也就是我们常说的越界问题.
比如,我们创建了一个数组 int a[] = new int[4] ;
那么数组a只能存放四个元素,而数组的下标是从0开始的,也就是说,a[3]就是最后一个元素.当你给a[4]赋值,或者使用a[4]的时候,就出现了ArrayIndexOutOfBoundsException异常.
看到了你补充的代码,发现了几点问题:你的类名肯定不是Point,而应该是points,而是你上面的类根本没有main函数,说明了你运行的就不是这个类,而是Point类,请你自己核对.