2015-10-26 171 views
0

我正在用zk and spring創建我的第一個應用程序。我描述了簡單的dao層和服務。問題是我的服務在viewmodel中始終爲空。我究竟做錯了什麼?zk + spring數據空指針異常

謝謝。

堆棧跟蹤

23:54:53.687 [http-bio-8080-exec-3] ERROR org.zkoss.bind.impl.ParamCall - java.lang.NullPointerException: null 
at ru.hostco.ced.viewmodel.TaskListViewModel.refreshTasks(TaskListViewModel.java:46) ~[classes/:na] 
at ru.hostco.ced.viewmodel.TaskListViewModel.init(TaskListViewModel.java:39) ~[classes/:na] 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_60] 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_60] 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_60] 
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_60] 
at org.zkoss.bind.impl.ParamCall.call(ParamCall.java:148) ~[zkbind-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.bind.impl.AbstractAnnotatedMethodInvoker.invokeMethod(AbstractAnnotatedMethodInvoker.java:90) [zkbind-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.bind.impl.BinderImpl.init(BinderImpl.java:336) [zkbind-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.bind.AnnotateBinder.init(AnnotateBinder.java:57) [zkbind-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.bind.BindComposer.doBeforeComposeChildren(BindComposer.java:166) [zkbind-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.zk.ui.impl.UiEngineImpl.doBeforeComposeChildren(UiEngineImpl.java:981) [zk-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild0(UiEngineImpl.java:920) [zk-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:877) [zk-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:748) [zk-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate(UiEngineImpl.java:709) [zk-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild0(UiEngineImpl.java:923) [zk-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:877) [zk-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:748) [zk-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate(UiEngineImpl.java:709) [zk-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild0(UiEngineImpl.java:923) [zk-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:877) [zk-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:748) [zk-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate(UiEngineImpl.java:709) [zk-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild0(UiEngineImpl.java:923) [zk-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:877) [zk-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:748) [zk-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:822) [zk-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:771) [zk-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate(UiEngineImpl.java:709) [zk-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage0(UiEngineImpl.java:452) [zk-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage(UiEngineImpl.java:358) [zk-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:217) [zk-8.0.0-Eval.jar:8.0.0] 
at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:136) [zk-8.0.0-Eval.jar:8.0.0] 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624) [servlet-api.jar:na] 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) [servlet-api.jar:na] 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) [catalina.jar:7.0.64] 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.64] 
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat7-websocket.jar:7.0.64] 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) [catalina.jar:7.0.64] 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.64] 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) [catalina.jar:7.0.64] 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) [catalina.jar:7.0.64] 
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) [catalina.jar:7.0.64] 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) [catalina.jar:7.0.64] 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) [catalina.jar:7.0.64] 
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) [catalina.jar:7.0.64] 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) [catalina.jar:7.0.64] 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423) [catalina.jar:7.0.64] 
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079) [tomcat-coyote.jar:7.0.64] 
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625) [tomcat-coyote.jar:7.0.64] 
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316) [tomcat-coyote.jar:7.0.64] 
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_60] 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_60] 
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-coyote.jar:7.0.64] 
at java.lang.Thread.run(Unknown Source) [na:1.8.0_60] окт 26, 2015 11:54:53 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [zkLoader] in context with path [/ced] threw exception java.lang.NullPointerException 
at ru.hostco.ced.viewmodel.TaskListViewModel.refreshTasks(TaskListViewModel.java:46) 
at ru.hostco.ced.viewmodel.TaskListViewModel.init(TaskListViewModel.java:39) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at org.zkoss.bind.impl.ParamCall.call(ParamCall.java:148) 
at org.zkoss.bind.impl.AbstractAnnotatedMethodInvoker.invokeMethod(AbstractAnnotatedMethodInvoker.java:90) 
at org.zkoss.bind.impl.BinderImpl.init(BinderImpl.java:336) 
at org.zkoss.bind.AnnotateBinder.init(AnnotateBinder.java:57) 
at org.zkoss.bind.BindComposer.doBeforeComposeChildren(BindComposer.java:166) 
at org.zkoss.zk.ui.impl.UiEngineImpl.doBeforeComposeChildren(UiEngineImpl.java:981) 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild0(UiEngineImpl.java:920) 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:877) 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:748) 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate(UiEngineImpl.java:709) 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild0(UiEngineImpl.java:923) 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:877) 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:748) 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate(UiEngineImpl.java:709) 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild0(UiEngineImpl.java:923) 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:877) 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:748) 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate(UiEngineImpl.java:709) 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild0(UiEngineImpl.java:923) 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:877) 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:748) 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:822) 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:771) 
at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate(UiEngineImpl.java:709) 
at org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage0(UiEngineImpl.java:452) 
at org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage(UiEngineImpl.java:358) 
at org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:217) 
at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:136) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) 
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423) 
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079) 
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625) 
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
at java.lang.Thread.run(Unknown Source) 

的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-4.2.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-4.2.xsd 
     http://www.springframework.org/schema/data/jpa 
     http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> 

<context:component-scan base-package="ru.hostco.ced"></context:component-scan> 

<bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="org.postgresql.Driver"></property> 
    <property name="url" 
     value="jdbc:postgresql://host:port/db"></property> 
    <property name="username" value="user"></property> 
    <property name="password" value="pass"></property> 
</bean> 

<bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="packagesToScan" value="ru.hostco.ced.spring.entity"></property> 
    <property name="dataSource" ref="dataSource"></property> 
    <property name="jpaProperties"> 
     <props> 
      <prop key="hibernate.show_sql">true</prop> 
      <prop key="hibernate.default_schema">ced</prop> 
     </props> 
    </property> 
    <property name="persistenceProvider"> 
     <bean class="org.hibernate.jpa.HibernatePersistenceProvider"></bean> 
    </property> 
</bean> 

<tx:annotation-driven transaction-manager="transactionManager" /> 

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
    <property name="dataSource" ref="dataSource"></property> 
</bean> 

<jpa:repositories base-package="ru.hostco.ced.spring.repository"></jpa:repositories> 

TaskRepository.java

public interface TaskRepository extends ReadOnlyRepository<Task, Integer> { 
@Query(value = "select * from ced.findtask(" 
     + "cast(:userLogin as text), " 
     + "cast(:dueDate as timestamp), " 
     + "cast(:group as text), " 
     + "cast(:subjectIds as text))", nativeQuery = true) 
public Set<Task> findByUserLoginAndDueDateAndGroupAndSubjects(
     @Param("userLogin") String userLogin, 
     @Param("dueDate") String dueDate, 
     @Param("group") String group, 
     @Param("subjectIds") String subjectIds); 
} 

TaskServiceImpl.java

@Transactional @Service("taskService") public class TaskServiceImpl implements TaskService { 
@Autowired 
private TaskRepository taskRepository; 

public Set<Task> findByFilters(String userLogin, Date dueDate, TaskGroup group, 
     Set<Integer> subjectIds) { 
    return taskRepository.findByUserLoginAndDueDateAndGroupAndSubjects(
      userLogin, 
      new SimpleDateFormat("yyyy-MM-dd").format(dueDate), 
      group.toString(), 
      subjectIds.toString().substring(1, subjectIds.toString().length() - 1).replace(" ", "")); 
} 

public Task findById(Integer id) { 
    return taskRepository.findOne(id); 
} 

public TaskRepository getTaskRepository() { 
    return taskRepository; 
} 

public void setTaskRepository(TaskRepository taskRepository) { 
    this.taskRepository = taskRepository; 
} 
} 

ViewModel.java

@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class) public class TaskListViewModel { @WireVariable 
private TaskServiceImpl taskService; 
private Set<Task> tasks; 
private Date selectedDate; 
private TaskGroup selectedGroup; 
private Set<Integer> subjectIds; 

/** 
* 
*/ 
@Init 
public void init() { 
    selectedGroup = TaskGroup.ASSIGN_TO_ME; 
    subjectIds = new HashSet<Integer>(); 
    subjectIds.add(TaskSubject.ALL_TASKS_SUBJECT.getId());  
    refreshTasks(); 
} 

@NotifyChange("tasks") 
@Command 
public void refreshTasks() { 
    tasks = new HashSet<Task>(); 
    tasks.addAll(taskService.findByFilters(User.TEST_USER_LOGIN, 
      selectedDate, selectedGroup, subjectIds)); 
} 

public Set<Task> getTasks() { 
    return tasks; 
} 

public void setTasks(Set<Task> tasks) { 
    this.tasks = tasks; 
} 

public Date getSelectedDate() { 
    return selectedDate; 
} 

@GlobalCommand 
public void setSelectedDate(@BindingParam("selectedDate") Date selectedDate) { 
    System.err.println(selectedDate.toString()); 
    this.selectedDate = selectedDate; 
} 

public TaskGroup getSelectedGroup() { 
    return selectedGroup; 
} 

@GlobalCommand 
public void setSelectedGroup(@BindingParam("selectedGroup")  
    TaskGroup selectedGroup) { 
    System.err.println(selectedGroup.toString()); 
    this.selectedGroup = selectedGroup; 
} 

public Set<Integer> getSubjectIds() { 
    return subjectIds; 
} 

public void setSubjectIds(Set<Integer> subjectIds) { 
    this.subjectIds = subjectIds; 
} 

public TaskServiceImpl getTaskService() { 
    return taskService; 
} 

public void setTaskService(TaskServiceImpl taskService) { 
    this.taskService = taskService; 
} 
} 
+0

什麼是taskservice的包名稱? – chillworld

+0

您是否使用簡單的Servlet嘗試過注入? – flo

回答

0

彈簧服務不被ZK知道,你需要添加:

<zksp:zk-config> 
</zksp:zk-config> 

到您的應用程序licationContext.xml。