我正在使用 Swing 开发一个桌面应用程序,但是我想使用 Spring 框架来使用 IOC,并且我想使用接口技术来做。但是当我通过接口的实例调用函数并且该接口由声明被调用函数的类实现时,我得到了 nullPointerException。
这是我的简单应用程序代码:
界面:
public interface IEmpMasterService {
public InternalResultsResponse<EmpMaster> maintainEmpMaster(
EmpMaster empMaster);
}
实现类:
public class EmpMasterService implements IEmpMasterService {
private IEmpMasterDAO empMasterDAO;
public IEmpMasterDAO getEmpMasterDAO() {
return empMasterDAO;
}
public void setEmpMasterDAO(IEmpMasterDAO empMasterDAO) {
this.empMasterDAO = empMasterDAO;
}
@Override
public InternalResultsResponse<EmpMaster> maintainEmpMaster(
EmpMaster empMaster) {
InternalResultsResponse<EmpMaster> response = new InternalResultsResponse<EmpMaster>();
//some logic her...
response = getEmpMasterDAO().maintainEmpMaster(request);
return response;
}}
DAO接口:
public interface IEmpMasterDAO {
InternalResultsResponse<EmpMaster> maintainEmpMaster(
EmpMasterRequest request);
}
class Implementing DAO:
public class EmpMasterDAO extends HibernateDaoSupport implements IEmpMasterDAO{
@Override
public InternalResultsResponse<EmpMaster> maintainEmpMaster(
EmpMasterRequest empMasterRequest) {
//hibernate code goes here...
return resultsResponse;
}
}
主类:
public class EmpManager extends javax.swing.JFrame {
private IEmpMasterService empMasterService;
private static ApplicationContext context;
public IEmpMasterService getEmpMasterService() {
return empMasterService;
}
public void setEmpMasterService(IEmpMasterService empMasterService) {
this.empMasterService = empMasterService;
}
public static void main(String args[]) {
context = new ClassPathXmlApplicationContext(new String[]{"ApplicationContext.xml"});
empMasterService = (EmpMasterService)context.getBean("empMasterService");
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String name = jTextField1.getText();
String salary = jTextField2.getText();
EmpMaster empMaster = new EmpMaster();
empMaster.setEmpName(name);
empMaster.setSalary(Double.parseDouble(salary));
//***** getting Error at this line *****//
InternalResultsResponse<EmpMaster> response = getEmpMasterService().maintainEmpMaster(empMaster);
}
我的 ApplicationContext.xml :
<?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-3.0.xsd">
<import resource="dataSource.xml"/>
<bean id="mainFrame" class="org.UI.EmpManager">
<property name="title"> <value>Spring Example</value></property>
<property name="empMasterService" ref="empMasterServiceImpl"></property>
</bean>
<bean id="empMasterServiceImpl" class="org.service.EmpMasterService">
<property name="empMasterDAO" ref="empMasterDAOTarget"></property>
</bean>
<bean id="empMasterDAOTarget" class="org.DAO.EmpMasterDAO">
<property name="hibernateTemplate" ref="matrixHibernateTemplate"/>
</bean>
</beans>
当我点击按钮时,我得到了一个错误。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at org.UI.EmpManager.jButton1ActionPerformed(EmpManager.java:140)
at org.UI.EmpManager.access$0(EmpManager.java:132)
at org.UI.EmpManager$1.actionPerformed(EmpManager.java:67)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)