如何解决线程“main”中的异常 java.lang.NullPointerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21626948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can I solve Exception in thread "main" java.lang.NullPointerException
提问by user2957980
I created two class
我创建了两个类
but I have problem
但我有问题
when I press run it is showing me this error
当我按运行时,它向我显示此错误
----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.
Exception in thread "main" java.lang.NullPointerException << I know what is this mean
线程“main”中的异常 java.lang.NullPointerException << 我知道这是什么意思
but I do not know how can I correct my code !
但我不知道如何更正我的代码!
this is first class
这是头等舱
class circle{
private double radius;
static int count=0;
public circle(){
radius=8.9;
}
public circle(double r){
radius=r;
count++;
}
public void setradius(double r){
radius = r;
}
public double getradius(){
return radius;
}
public double area (){
double area1 = (radius*radius*(Math.PI));
return area1;
}
public double circumference(){
double circumference1 = (Math.PI)*radius;
return circumference1;
}
public static double areas(double r){
double area2 = (r*r*(Math.PI));
return area2;
}
}
and this is the second one
这是第二个
class client{
public static void main (String args[]){
circle array[] = new circle [10];
for (int i = 0 ; i<10 ; i++){
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());
System.out.println(circle.areas(35));
}
}
}
采纳答案by Kristian Duske
You are incrementing i
twice in your loop:
您i
在循环中增加了两次:
for (int i = 0 ; i<10 ; i++){
array [i]= new circle (i++);
The result is that you are trying to access a circle in your array which has not yet been initialized (all array contents point to NULL
initially). I'm sure you mean to write
结果是您试图访问数组中尚未初始化的圆(所有数组内容NULL
最初都指向)。我确定你是想写
for (int i = 0 ; i<10 ; i++){
array [i]= new circle (i + 1);
回答by ohlmar
Because this line is increasing the value of i by 1. And the row after is using the next position in the array which is empty.
因为这一行将 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());
回答by pL4Gu33
array [i]= new circle (i++);
you count i one up. But the next i value is null.
你算我一个。但是下一个 i 值为空。
回答by MT0
You are incrementing i
in the for
loop and when you initialise each circle:
您i
在for
循环中递增,并在初始化每个圆圈时:
for (int i = 0 ; i<10 ; i++){
array [i]= new circle (i++);
When you go to output the circle you are then referring to an uninitialised array entry.
当你去输出圆时,你指的是一个未初始化的数组条目。
You need to remove one of these:
您需要删除其中之一:
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));
}
回答by edubriguenti
The problem is with this line:
问题出在这一行:
array[i] = new circle(i++);
You are adding i + 1 again.
您再次添加 i + 1 。
Just do this:
只需这样做:
array[i] = new circle(i);
回答by Vin.AI
This could be your solution:
这可能是您的解决方案:
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));
}
}
}