我正在使用两个bean类employee和admin。我已使用employee类对象在employee类中设置变量empid。我想将该employee类对象(emp)设置为admin class.but我得到空指针异常在empid领域。
emp.setEmpid(eid);
ad.setCourseid(cid);
ad.setCoursename(cname);
ad.setParticipants(participants);
ad.setEmployee(emp);
员工
package entity;
public class Employee
{
private int empid;
private String empname;
private String password;
private String designation;
private String domain;
private String role;
private String head;
private String specialist;
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}
public String getEmpname() {
return empname;
}
public void setEmpname(String empname) {
this.empname = empname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getHead() {
return head;
}
public void setHead(String head) {
this.head = head;
}
public String getSpecialist() {
return specialist;
}
public void setSpecialist(String specialist) {
this.specialist = specialist;
}
}
管理
package entity;
public class Admin {
private int courseid;
private String coursename;
private int participants;
private Employee employee;
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee empid) {
this.employee = employee;
}
public int getCourseid() {
return courseid;
}
public void setCourseid(int courseid) {
this.courseid = courseid;
}
public String getCoursename() {
return coursename;
}
public void setCoursename(String coursename) {
this.coursename = coursename;
}
public int getParticipants() {
return participants;
}
public void setParticipants(int participants) {
this.participants = participants;
}
}
实际上我想将数据库中的empid作为外键插入,但是由于空指针异常,我不能这样做。
答案 0 :(得分:0)
看看这里,在您的管理类中,您正在做这样的事情
public void setEmployee(Employee empid) {
this.employee = employee;
}
这就像空任务一样,您将员工分配给没有任何效果的员工,总是将空值分配给空值,因为对象的默认值为空
就像
一样public void setEmployee(Employee empid) {
this.employee = empid;
}