java问题,为什么运行时输入会出现空指针异常.....

import java.util.Scanner;

class Qujian{
float x1,x2,y1,y2;
Qujian(float x1,float x2,float y1,float y2){
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}
boolean include(Qujian q){
if(this.x1<=q.x1&&this.x2>=q.x2&&this.y1<=q.y1&&this.y2>=q.y2)return true;
else return false;
}
}
public class Zju1139 {
public static void main(String args[])throws Exception{
Scanner in = new Scanner(System.in);
while(in.hasNext()){
int a = in.nextInt();
int count = 0;
Qujian[] q = new Qujian[a];
for(int i=0;i<a;i++){
q[i].x1 = in.nextFloat();
q[i].x2 = in.nextFloat();
q[i].y1 = in.nextFloat();
q[i].y2 = in.nextFloat();
}
for(int i=0;i<a;i++){
for(int j=0;j<a;j++){
if(i!=j){
if(q[i].include(q[j]))count++;
}
}
}
System.out.println(count);
}
}
}

for(int i=0;i<a;i++){
//替换下面几句,因为q[i]还没有初始化
q[i] = new Qujian(in.nextFloat();,in.nextFloat(),in.nextFloat(),in.nextFloat(),in.nextFloat());
//q[i].x1 = in.nextFloat();
//q[i].x2 = in.nextFloat();
//q[i].y1 = in.nextFloat();
//q[i].y2 = in.nextFloat();
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-03-31
首先要加一个无参构造函数。
然后修改for循环,增加一个初始化语句,如下:
for(int i=0;i<a;i++){
q[i] = new Qujian(); //新增
q[i].x1 = in.nextFloat();
相似回答