0
每當我嘗試向數據庫中插入數據時,我都會得到NPE(空指針異常)。幫助我解決這個問題。我已經導入了春季需要的所有必需的庫。 這是我的applicationContext.xml文件,我配置了所有的bean。spring 2.5.6 jsf應用程序中的DI問題
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="personService" class="com.ecommerce.assignment.service.PersonService">
<property name="personDAO" ref="personDAO" />
</bean>
<bean id="personBean" class="com.ecommerce.assignment.bean.PersonBean" />
<bean id="personDAO" class="com.ecommerce.assignment.dao.PersonDAO" />
</beans>
這是人DAO的界面。
public interface IPersonDAO {
public void addPerson(Person instance);
}
此的PersonDAO類實現IpersonDAO接口及未實現的方法。
public class PersonDAO implements IPersonDAO {
@Override
public void addPerson(Person instance) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction().begin();
session.saveOrUpdate(instance);
session.getTransaction().commit();
session.close();
}
}
這是接口personService。 public interface IPersonService {public void addPerson(Person instance); }
public class PersonService implements IPersonService,Serializable {
private IPersonDAO personDAO;
public IPersonDAO getPersonDAO() {
return personDAO;
}
public void setPersonDAO(IPersonDAO personDAO) {
this.personDAO = personDAO;
}
@Override
public void addPerson(Person instance) {
getPersonDAO().addPerson(instance);
}
}
public class PersonBean implements Serializable {
private static final long serialVersionUID = 1L;
private String emailAddress;
private String password;
private String username;
@ManagedProperty(value = "#{personService}")
private IPersonService iPersonService;
public void addPerson(){
try{
Person person = new Person();
person.setUsername(getUsername());
person.setEmailAddress(getEmailAddress());
person.setPassword(getPassword());
iPersonService.addPerson(person);
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
你忘了告訴哪個變量完全是'null'。請相應更新問題。或者你的具體問題是你不明白'NullPointerException'是什麼意思? – BalusC
@BalusC,它是給NPE的iPersonService。 – Umair