帮忙解决一下这个空指针问题!!!!!!!!

帮忙解决一下这个空指针问题!!!!!!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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;
    }
     
 
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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;
            }
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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());
*/
    }
 
}


正在回答

登陆购买课程后可参与讨论,去登陆

2回答

首先:空指针异常是因为有值为null的对象去调用了方法或属性:

如下图报错信息提示的

http://img1.sycdn.imooc.com//climg/5c0f609e0001cbe307390110.jpg

Department 中的第66行

我们一起看一下第66行:

http://img1.sycdn.imooc.com//climg/5c0f60ed0001ca6a08380194.jpg

首先我们看到66行中调用了方法或属性的有 this 或者this.getMyStaff();

因为this就是当前对象,所以this不可能为null,那么应该是this.getMyStaff()是null,调用length的时候发生了空指针异常:

我们往上看一下:

http://img1.sycdn.imooc.com//climg/5c0f619c000109e707240257.jpg

http://img1.sycdn.imooc.com//climg/5c0f61aa00019f7e04200096.jpg

我们发现这个数组一直没有被初始化过,所以通过get方法的到的也就是null;

解决办法:

1、我们可以在对应的get方法中给它初始化一下。

2、也可以在设置这个属性的时候直接初始化一下

同学可以参考一下老师初始化学生数组的时候:

http://img1.sycdn.imooc.com//climg/5c0f63220001783c06460162.jpg

老师的视频 3-2 新增属性完成学生信息存储2分10秒开始有讲过这个问题。老师是在get方法中初始化了数组。同学可以回顾一下。

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!



  • 有点麋鹿 提问者 #1
    非常感谢,没有注意到这个细节!
    2018-12-11 16:02:14
提问者 有点麋鹿 2018-12-11 14:23:22

测试类中注释掉的那部分传入值后代码显示null,是怎么回事?做的我头懵。求指教!!!!!

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码