public class Range_Count {
private int range;
private int count;
public Range_Count(){
setRange();
setCount();
}
public Range_Count(int r , int c){
range = r;
count = c;
}
public Range_Count(Range_Count rc){
range = rc.getRange();
count = rc.getCount();
}
public void setRange(){
range = 0;
}
public void setRange(int s){
range = s;
}
public void setCount(){
count = 0;
}
public void setCount(int c){
count = c;
}
public int getRange(){
return range;
}
public int getCount(){
return count;
}
public void displayRange_Count(){
System.out.print(" Range: "+range);
System.out.print(" Count: "+count+"\n");
}
}
public class Attribute{
private String attribute;
private Range_Count[] range;
public Attribute(){
setAttribute();
setRange_Count();
}
public Attribute(String s , Range_Count[] rc){
setAttribute(s);
setRange_Count(rc);
}
public Attribute(String s, int[] s1, int[] s2){
setAttribute(s);
setRange_Count(s1,s2);
}
public void setAttribute(){
attribute = null;
}
public void setAttribute(String s){
attribute = s;
}
public void setRange_Count(){
range = new Range_Count[5];
for(int i = 0 ; i < range.length ; i++)//range.length->5
range[i]=new Range_Count();
}
public void setRange_Count(Range_Count[] s){
range = s;
}
public void setRange_Count(int[] s1 , int[] s2){
for(int i = 0 ; i < s1.length ; i++){
range[i].setRange(s1[i]);
range[i].setCount(s2[i]);
}
}
public String getAttribute(){
return attribute;
}
public Range_Count[] getRange(){
return range;
}
public Range_Count getRange(int i){
return range[i];
}
public void displayAttri(){
System.out.println("Attribute: "+getAttribute());
for(int i = 0 ; i < range.length ; i++)//range.length->5
range[i].displayRange_Count();
System.out.println("");
}
public static void main(String[] args){
Attribute a = new Attribute();
int s1[] = {1 ,2 , 3 ,4 , 5};
int s2[] = {2 ,4 , 5 , 6, 7};
//Range_Count[] r = new Range_Count[5];
/*for(int i = 0 ; i < r.length ; i++){
r[i] = new Range_Count(i+1,i+3);
}*/
a=new Attribute("rbs",s1,s2);
a.displayAttri();
}
}
出现下面的错误
java.lang.NullPointerException
at Attribute.setRange_Count(Attribute.java:45)
at Attribute.<init>(Attribute.java:22)
at Attribute.main(Attribute.java:78)
Exception in thread "main"
应如何调试