3-8作业问题
package com.Imooc;
public class Department {
private int departmentid;
private String departmentname;
private Staff[] myStaff;
private int staffNum;
public Department(){
}
public Department(int departmentid,String departmentname){
this.departmentid =departmentid;
this.departmentname =departmentname;
}
public int getDepartmentid() {
return departmentid;
}
public String getDepartmentname() {
return departmentname;
}
public void setDepartmentid(int departmentid) {
this.departmentid = departmentid;
}
public void setDepartmentname(String departmentname) {
this.departmentname = departmentname;
}
public Staff[] getMyStaff() {
if(this.myStaff==null)
this.myStaff =new Staff[20];
return myStaff;
}
public int getStaffNum() {
return staffNum;
}
public void setMyStaff(Staff[] myStaff) {
this.myStaff = myStaff;
}
public void setPostNu(int staffNum) {
this.staffNum = staffNum;
}
public void addStaff(Staff sta){
for(int i=0;i<this.getMyStaff().length;i++){
if(this.getMyStaff()[i]==null){
this.getMyStaff()[i]=sta;
this.staffNum=i+1;
return;
}
}
}
}package com.Imooc;
public class Staff {
//成员属性:员工姓名,工号,年龄,性别,所属部门,职务信息
private String staffname;
private String staffid;
private int staffage;
private String staffsex;
private Department staffdepartment;
private Post staffpost;
//无参构造方法
public Staff(){
}
//带参构造方法,实现对属性的全部赋值
public Staff(String staffname,String staffid,int staffage,String staffsex){
this.staffname=staffname;
this.staffid =staffid;
this.setStaffage(staffage);
this.setStaffsex(staffsex);
}
public String getStaffname() {
return staffname;
}
public String getStaffid() {
return staffid;
}
public int getStaffage() {
return staffage;
}
public String getStaffsex() {
return staffsex;
}
public Department getStaffdepartment() {
return staffdepartment;
}
public Post getStaffpost() {
return staffpost;
}
public void setStaffname(String staffname) {
this.staffname = staffname;
}
public void setStaffid(String staffid) {
this.staffid = staffid;
}
//设置年龄限制,必须在18-65之间,否则强制默认为18
public void setStaffage(int staffage) {
if(staffage>=18&&staffage<=65){
this.staffage = staffage;
}
else{
this.staffage=18;
}
}
//设定性别只能是“男”或“女”,反之则设置默认为“男”
public void setStaffsex(String staffsex) {
if(staffsex.equals("女")){
this.staffsex = staffsex;
}
else{
this.staffsex = "男";
}
}
public void setStaffdepartment(Department staffdepartment) {
this.staffdepartment = staffdepartment;
}
public void setStaffpost(Post staffpost) {
this.staffpost = staffpost;
}
public String info(Department staffdepartment, Post staffpost){
String str="姓名:"+this.getStaffname()+"\n工号"+this.getStaffid()+"\n年龄"+this.getStaffage()+"\n性别"+this.getStaffsex()+"\n职务:"+this.getStaffdepartment().getDepartmentname()+this.getStaffpost().getPostname();
return str;
}
}package com.Imooc;
public class Post {
private int postid;
private String postname;
public Post(){
}
public Post(int postid,String postname){
this.postid=postid;
this.postname=postname;
}
public int getPostid() {
return postid;
}
public String getPostname() {
return postname;
}
public void setPostid(int postid) {
this.postid = postid;
}
public void setPostname(String postname) {
this.postname = postname;
}
}package com.Imooc;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Department dep1=new Department(1,"人事部");
Department dep2=new Department(2,"市场部");
Post po1=new Post(1,"经理");
Post po2=new Post(2,"助理");
Post po3=new Post(3,"职员");
Staff sta1=new Staff("张铭","S001",29,"男");
Staff sta2=new Staff("李艾爱","S002",21,"女");
Staff sta3=new Staff("孙超","S004",29,"男");
Staff sta4=new Staff("张美美","S005",26,"女");
Staff sta5=new Staff("蓝迪","S006",37,"男");
Staff sta6=new Staff("米莉","S007",24,"女");
dep1.addStaff(sta1);
dep1.addStaff(sta2);
dep1.addStaff(sta3);
dep2.addStaff(sta4);
dep2.addStaff(sta5);
dep2.addStaff(sta6);
System.out.println(sta1.info(dep1,po1));
System.out.println("=================================");
System.out.println(sta1.info(dep1,po2));
System.out.println("=================================");
System.out.println(sta1.info(dep1,po3));
System.out.println("=================================");
System.out.println(sta1.info(dep2,po3));
System.out.println("=================================");
System.out.println(sta1.info(dep2,po1));
System.out.println("=================================");
System.out.println(sta1.info(dep2,po3));
System.out.println("=================================");
System.out.println("人事部总共有"+dep1.getStaffNum()+"名员工");
System.out.println("市场部总共有"+dep2.getStaffNum()+"名员工");
}
}Exception in thread "main" java.lang.NullPointerException at com.Imooc.Staff.info(Staff.java:75) at com.Imooc.Test.main(Test.java:24)
为什么会出现空指针错误,哪里出错了?该改成什么样?
58
收起
正在回答
2回答
因为你Staff类的初始化并没有给对应的Department和Post赋值,而info()方法中你传递了部门和职务对象,但是获取值的时候部门和职务是通过Staff员工类中的部门和职务熟悉的get方法获取的,但是并没有set,所以无法获取这两个属性的值。两种修改方法都可以:
1、在Staff类中的info()方法中,直接通过staffdepartment和staffpost两个参数对象调用对应的get方法获取名称即可。
2、在Staff类中将构造方法的参数在加入部门和职务对象参数,然后在Test类中初始化员工对象的时候,可以将对应的部门和职务对象传递进去即可。
上述方式可以选择一种进行修改。
Android零基础入门2018版
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7235 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星