帮忙解决一下这个空指针问题!!!!!!!!
package com.imooc.infomanage; public class Duty { private String dutyNum; private String dutyName; //无参构造方式 public Duty() { } //有参构造方式 public Duty(String dutyNum,String dutyName) { this.setDutyNum(dutyNum); //调用set方法传值 this.setDutyName(dutyName); } //封装变量set、get方法 public String getDutyNum() { return dutyNum; } public void setDutyNum(String dutyNum) { this.dutyNum = dutyNum; } public String getDutyName() { return dutyName; } public void setDutyName(String dutyName) { this.dutyName = dutyName; } //展示信息方法 public String showInfor() { String str="工号:"+this.getDutyNum()+"\n职务:"+this.getDutyName(); return str; } }
package com.imooc.infomanage; public class Staff { private String staName; private String staSex; private int staAge; private Department myDep; private Duty myDuty; // 无参构造方式 public Staff() { } // 三参构造方式 public Staff(String staName, String staSex, int staAge) { this.setStaName(staName); this.setStaSex(staSex); this.setStaAge(staAge); } //五参 public Staff(String staName,String staSex, int staAge,Department myDep,Duty myDuty) { this.setStaName(staName); this.setStaSex(staSex); this.setStaAge(staAge); this.setMyDep(myDep); this.setMyDuty(myDuty); } public Department getMyDep() { return myDep; } public void setMyDep(Department myDep) { if (this.myDep == null) { this.myDep = new Department(); } else this.myDep = myDep; } public Duty getMyDuty() { return myDuty; } public void setMyDuty(Duty myDuty) { if (this.myDuty == null) { this.myDuty = new Duty(); } else this.myDuty = myDuty; } // 封装变量,set、get方法 public void setStaName(String staName) { this.staName = staName; } public String getStaName() { return staName; } public String getStaSex() { return staSex; } public void setStaSex(String staSex) { if (staSex != "男" & staSex != "女") { this.staSex = "男"; } else this.staSex = staSex; } public int getStaAge() { return staAge; } public void setStaAge(int staAge) { if (staAge < 18 || staAge > 65) { this.staAge = 18; } else this.staAge = staAge; } // 描述员工姓名、工号、年龄、性别、所属部门、职务信息 public String showSta() { String str = "姓名:" + this.getStaName() + "\n工号:" + this.getMyDuty().getDutyNum() + "\n性别:" + this.getStaSex() + "\n年龄:" + this.getStaAge() + "\n职务:" + this.getMyDep().getDepName() + this.getMyDuty().getDutyName(); return str; } public String showSta(String dutyNum, String depName, String dutyName) { String str = "姓名:" + this.getStaName() + "\n工号:" + dutyNum + "\n性别:" + this.getStaSex() + "\n年龄:" + this.getStaAge() + "\n职务:" + depName + dutyName; return str; } public String showSta(Department dep, Duty dut) { String str = "姓名:" + this.getStaName() + "\n工号:" + dut.getDutyNum() + "\n性别:" + this.getStaSex() + "\n年龄:" + this.getStaAge() + "\n职务:" + dep.getDepName() + dut.getDutyName() + "\n==========="; return str; } }
package com.imooc.infomanage; public class Department { private String depNum; private String depName; private Staff[] myStaff; private int staCount; public Department() { } // 有参构造方法 public Department(String depName) { this.setDepName(depName); } // 有参构造方法:包含部门名字、部门员工 public Department(String depName, Staff[] myStaff) { this.setDepName(depName); this.setMyStaff(myStaff); } // 封装属性set、get方法 public int getStaCount() { return staCount; } public void setStaCount(int staCount) { this.staCount = staCount; } public String getDepNum() { return depNum; } public void setDepNum(String depNum) { this.depNum = depNum; } public String getDepName() { return depName; } public void setDepName(String depName) { this.depName = depName; } public Staff[] getMyStaff() { return myStaff; } public void setMyStaff(Staff[] myStaff) { if (this.myStaff == null) { this.myStaff = new Staff[200]; } else this.myStaff = myStaff; } public String showDep() { String str = "部门:" + this.getDepName(); return str; } public void staSum(Staff sta) { for (int i = 0; i<this.getMyStaff().length; i++) { if (this.getMyStaff()[i] == null) { sta.setMyDep(this); this.getMyStaff()[i] = sta; this.staCount = i + 1; return; } } } }
package com.imooc.test; import com.imooc.infomanage.Department; import com.imooc.infomanage.Duty; import com.imooc.infomanage.Staff; public class Test { public static void main(String[] args) { Staff sta1=new Staff("张铭","男",29); Staff sta2=new Staff("李艾爱","女",21); Staff sta3=new Staff("孙超","男",29); Staff sta4=new Staff("张美美","女",26); Staff sta5=new Staff("蓝迪","男",37); Staff sta6=new Staff("米莉","女",24); Department dep1=new Department("人事部"); Department dep2=new Department("市场部"); Duty dut1=new Duty("S001","经理"); Duty dut2=new Duty("S002","助理"); Duty dut3=new Duty("S004","职员"); Duty dut4=new Duty("S005","职员"); Duty dut5=new Duty("S006","经理"); Duty dut6=new Duty("S007","职员"); System.out.println(sta1.showSta(dep1, dut1)); System.out.println(sta2.showSta(dep1, dut2)); System.out.println(sta3.showSta(dep1, dut3)); System.out.println(sta4.showSta(dep2, dut4)); System.out.println(sta5.showSta(dep2, dut5)); System.out.println(sta6.showSta(dep2, dut6)); dep1.staSum(sta1); dep1.staSum(sta2); dep1.staSum(sta3); dep2.staSum(sta4); dep2.staSum(sta5); dep2.staSum(sta6); System.out.println(dep1.getDepName()+"总共有"+dep1.getStaCount()+"名员工"); System.out.println(dep2.getDepName()+"总共有"+dep2.getStaCount()+"名员工"); /* Department dep1 = new Department("人事部"); Duty dut1 = new Duty("S001", "经理"); Staff sta1 = new Staff("张铭", "男", 29, dep1, dut1); System.out.println(sta1.showSta()); System.out.println(dut1.showInfor()); System.out.println(dep1.showDep()); */ } }
0
收起
正在回答
2回答
首先:空指针异常是因为有值为null的对象去调用了方法或属性:
如下图报错信息提示的
Department 中的第66行
我们一起看一下第66行:
首先我们看到66行中调用了方法或属性的有 this 或者this.getMyStaff();
因为this就是当前对象,所以this不可能为null,那么应该是this.getMyStaff()是null,调用length的时候发生了空指针异常:
我们往上看一下:
我们发现这个数组一直没有被初始化过,所以通过get方法的到的也就是null;
解决办法:
1、我们可以在对应的get方法中给它初始化一下。
2、也可以在设置这个属性的时候直接初始化一下
同学可以参考一下老师初始化学生数组的时候:
老师的视频 3-2 新增属性完成学生信息存储2分10秒开始有讲过这个问题。老师是在get方法中初始化了数组。同学可以回顾一下。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星