Exception in thread "main" java.lang.NullPointerException


abstract class Dept
{
    String id;
    String name;
    String type;
    String description;
    Dept(){};
    Dept(String id,String type,String description){
        id=this.id;
        type=this.type;
        description=this.description;
    }
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getDesciption() {
        return description;
    }
    public void setDesciption(String desciption) {
        this.description = desciption;
    }
    
}

class OutpatientDept extends Dept
{
    String adress;
    OutpatientDept(){}
    OutpatientDept(String id,String name,String type,String adress){
        id=this.id;
        name=this.name;
        type=this.type;
        adress=this.adress;
    }
    public String toString() {
        return "编号:"+id+"名称:"+name+"类型:"+type+"描述:"+description+"地址:"+adress;
    }    
}

class InpatientDept extends Dept{
    String ward;
    InpatientDept(){}
    InpatientDept(String id,String name,String type,String ward){
        id=this.id;
        name=this.name;
        type=this.type;
        ward=this.ward;
    }
    public String toString() {
        return "编号:"+id+"名称:"+name+"类型:"+type+"描述:"+description+"病区:"+ward;
    }    
}

class Hospital
{    
    static Dept[] Array=new Dept[100];
    static void showDept(Dept Array[]) {
        for(int i=0;i<Array.length;i++)
        {
            Array[i].toString();
        }
    }
    static void addDept(Dept Array[],Dept addDept){
        int i=Array.length;
        Array[i]=addDept;
    }
    static void delDept(Dept Array[],int d){
        for(int i=d;i<Array.length;i++)
        {
            Array[i-1]=Array[i];
        }
    }
}

public class Mock{
    public static void main(String[] args) {     
        OutpatientDept o1=new OutpatientDept("2021001","001","aa","aaaa");
        OutpatientDept o2=new OutpatientDept("2021002","002","bb","bbbb");
        OutpatientDept o3=new OutpatientDept("2021003","003","cc","cccc");
        InpatientDept i1=new InpatientDept("20210001","1001","aaa","aaaaa");
        InpatientDept i2=new InpatientDept("20210002","1002","bbb","aaaaa");
        InpatientDept i3=new InpatientDept("20210003","1003","bbb","aaaaa");
        Hospital.Array[0]=o1;
        Hospital.Array[1]=o2;
        Hospital.Array[2]=o3;
        Hospital.Array[3]=i1;
        Hospital.Array[4]=i2;
        Hospital.Array[5]=i3;        
            Hospital.showDept(Hospital.Array);
    }
}


img

是重写的toString函数调用失败吗

static Dept[] Array=new Dept[100];这个数组题主只赋值了6个项,其余的94个并没有赋值会为null,取第6个时为null,调用toString就出错了。showDept应该 判断不是null再调用toString()方法。有帮助或启发麻烦点个采纳【本回答右上角】,谢谢~~

    static void showDept(Dept Array[]) {
        for(int i=0;i<Array.length;i++)
        if(Array[i]!=null)/////////
        {
            Array[i].toString();
        }
    }

而且题主的构造函数赋值全部反了,应该给实例属性赋值,而不是给构造函数的参数赋值为类属性

img


img

你Array.length值为100,但你只有0--5有元素