报错java.lang.NullPointerException,求指点

qq_36567914 2017-03-18 10:25:07
在输出单个建站性价比时OK,但后面就报错了,应该是134,135行有错,但不知道怎么改,请教大神
import java.util.*;

class site implements Cloneable{

char name;
int cost ;
int sale ;
site[] relations;


site(char a,int c,int b){

this.name = a;
this.cost = c;
this.sale = b;

}
site(site s){
this.name = s.name;
this.cost = s.cost;
this.sale = s.sale;
this.relations = s.relations;


}

void set_relations(site[] s){
this.relations = s;
}

int saleadditor(){
int sum = this.sale;
for(int i = 0;i<relations.length;i++){

sum +=relations[i].sale;
}
return sum;
}

double caculator(){
int sum = saleadditor();
return (double)sum*2/this.cost;
}

protected Object clone(){
site s = null;
try {
s = (site)super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return s;
}


}

public class cost_performance {

static int check(site[] s, site a,int n){ //n表示现有数组中含有的非空元素个数
boolean check = false;
for(int i = 0;i<n;i++){
if(a.name == s[i].name)
check = true;
}
if(check ==false){
s[n] = new site(a);
n++;
}
return n;
}



public static void main(String args[]){

site A,B,C,D,E,F,G;

A = new site('A',240,700);
B = new site('B',280,500);
C = new site('C',220,1100);
D = new site('D',260,800);
E = new site('E',380,600);
F = new site('F',250,1400);
G = new site('G',200,1600);
site[] r1 = {B,D};
site[] r2 = {A,C,E};
site[] r3 = {B,E,F};
site[] r4 = {A,E};
site[] r5 = {B,C,D,F,G};
site[] r6 = {C,E};
site[] r7 = {E};
A.set_relations(r1);
B.set_relations(r2);
C.set_relations(r3);
D.set_relations(r4);
E.set_relations(r5);
F.set_relations(r6);
G.set_relations(r7);
site[] all = {A,B,C,D,E,F,G};

System.out.println("A单独建站的性价比为:"+A.caculator());
System.out.println("B单独建站的性价比为:"+B.caculator());
System.out.println("C单独建站的性价比为:"+C.caculator());
System.out.println("D单独建站的性价比为:"+D.caculator());
System.out.println("E单独建站的性价比为:"+E.caculator());
System.out.println("F单独建站的性价比为:"+F.caculator());
System.out.println("G单独建站的性价比为:"+G.caculator());

for(int i = 0; i<7;i++){


for(int j = 1+i;j<7;j++){
site[] box = new site[7];
box[0] =new site(all[i]);
int num = 1;
for(int a = 0;a<all[i].relations.length;a++){
box[1+i] = new site(all[i].relations[i]);
num++;
}

num = check(box,all[j],num);


for(int b=0;b<all[j].relations.length;b++){

num = check(box,all[j].relations[b],num);

}

int salenum = 0;
int totalcost = 0;
for(int y = 0;y<num;y++){
salenum = salenum+ box[y].sale;
totalcost = totalcost+ box[y].cost;
}
double costperformance = 2*salenum/totalcost;
System.out.println("在" + all[i].name +"和"+all[j].name+"处建站的性价比为"+costperformance);
}

}

}
}
...全文
276 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
半城煙沙 2017-03-20
  • 打赏
  • 举报
回复
楼上+1,做任何运算之前要对运算的参数做判断,保证不能为 空
作现 2017-03-20
  • 打赏
  • 举报
回复
一楼正解,你要保证你的参数不能为null
墨笙弘一 2017-03-20
  • 打赏
  • 举报
回复
楼上+1,很容易忽略这种情况,如果它是null,然后你对null调用其他的方法或者进行运算,当然是会报空指针
tom_66 2017-03-19
  • 打赏
  • 举报
回复
box[y] 要判断是否为空,后面不为空,属性做加减运算的时候也要判断空 box[y].sale box[y].cost

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧