qq_51371177 2021-10-15 22:56 采纳率: 0%
浏览 105

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函数调用失败吗

  • 写回答

2条回答 默认 最新

  • CSDN专家-showbo 2021-10-15 23:11
    关注

    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

    评论

报告相同问题?

问题事件

  • 创建了问题 10月15日

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码