如何解决线程中的异常;“主要”;java.lang.NullPointerException
我创建了两个类 但我有问题 当我按run时,它会显示此错误如何解决线程中的异常;“主要”;java.lang.NullPointerException,java,nullpointerexception,Java,Nullpointerexception,我创建了两个类 但我有问题 当我按run时,它会显示此错误 ----jGRASP exec: java client Exception in thread "main" java.lang.NullPointerException at client.main(client.java:7) ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. 线程“main”java.lang
----jGRASP exec: java client
Exception in thread "main" java.lang.NullPointerException
at client.main(client.java:7)
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
线程“main”java.lang.NullPointerException中的异常您正在循环中递增
i
两次:
for (int i = 0 ; i<10 ; i++){
array [i]= new circle (i++);
因为这条线将i的值增加1。后面的行使用数组中的下一个空位置
array [i]= new circle (i++);
System.out.println("The area of the circle of radius "+ array[i].getradius()+"is" + array[i].area() + "and the circumference is" + array[i].circumference());
你算我一个。但是下一个i值为空。在
for
循环中,您正在增加i
,当您初始化每个圆时:
for (int i = 0 ; i<10 ; i++){
array [i]= new circle (i++);
for(int i=0;i问题在于这行:
array[i] = new circle(i++);
您再次添加i+1
只要这样做:
array[i] = new circle(i);
这可能是您的解决方案:
class client{
public static void main (String args[]) {
circle c[] = new circle[10];
for (int i = 0 ; i<10 ; i++) {
c[i] = new circle(i + 1);
System.out.println("The area of the circle of radius "+ c[i].getradius()+"is" + c[i].area() + "and the circumference is" + c[i].circumference());
System.out.println(c[i].areas(35));
}
}
}
类客户端{
公共静态void main(字符串参数[]){
圈c[]=新圈[10];
对于(int i=0;i@pedromss两个系统输出中的一个,我think@pedromssclientuse类中的第7行使用i代替i++,因为它将i的值增加1,但我不想发送0我想发送1@edubliguentiy您需要创建另一个变量。创建一个“k”变量。不要使用i,因为这是在for循环中递增的一个。哦,我不能像这样使用它[i]=新的圆(i+1);?!@edubligumentiyes,你可以这样使用。c[i]=新的圆(i);然后像新的圆(i+1);
for (int i = 0 ; i<10 ; i++){
array [i]= new circle (i++);
for (int i = 0 ; i<10 ; i++){
array [i]= new circle (i+1);
System.out.println("The area of the circle of radius "+ array[i].getradius()+"is" + array[i].area() + "and the circumference is" + array[i].circumference());
System.out.println(circle.areas(35));
}
array[i] = new circle(i++);
array[i] = new circle(i);
class client{
public static void main (String args[]) {
circle c[] = new circle[10];
for (int i = 0 ; i<10 ; i++) {
c[i] = new circle(i + 1);
System.out.println("The area of the circle of radius "+ c[i].getradius()+"is" + c[i].area() + "and the circumference is" + c[i].circumference());
System.out.println(c[i].areas(35));
}
}
}