2014-06-11 30 views
0

ERROR的NullPointerException在setAsText功能UserPropertyEditor(春季3.2)

java.lang.NullPointerException 
    at com.javalabs.web.dao.UserPropertyEditor.setAsText(UserPropertyEditor.java:20) 
... 

我無法將參數從表單屬性綁定userResponsible哪裏獲得一個用戶的ID綁定到對象任務。 但我得到參數userResponsible = 1,這是一個來自數據庫的用戶。從什麼可能是什麼想法?

form.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 

<h1>Create task</h1> 
<form:form method="GET" class="form-horizontal" 
    action="${pageContext.request.contextPath}/docreatetask" 
    commandName="task"> 
... 
    <div class="form-group"> 
     <label for="userresponsible" class="col-sm-2 control-label">User 
      responsible</label> 
     <div class="col-sm-10"> 
      <form:select path="userResponsible" name="userResponsible" class="form-control"> 
       <form:option value="0" label="Select" /> 
       <form:options items="${users}" itemValue="idUser" 
        itemLabel="username" /> 
      </form:select> 
      <div id="state.error"> 
       <span class="text-danger"><form:errors path="userResponsible" /></span> 
      </div> 
     </div> 
    </div> 
... 

Task.java

@Entity 
@Table(name = "t_task") 
public class Task { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "idTask") 
    private long idTask; 
    ... 
    @ManyToOne 
    @JoinColumn(name = "idUser_responsible", nullable = true) 
    private User userResponsible; 
    ... 

UserPropertyEditor.java

public class UserPropertyEditor extends PropertyEditorSupport { 

     private UserService userService; 

     @Autowired 
     public void setUserService(UserService userService) { 
      this.userService = userService; 
     } 

     @Override 
     public void setAsText(String text) throws IllegalArgumentException { 
line 20:  super.setValue(userService.get(Long.parseLong(text))); 
       //text variable is null when this function is called 
      } 
    } 

TaskController.java

... 
@InitBinder 
public void initBinder(WebDataBinder binder, HttpServletRequest req) { 

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd"); 
    dateFormat.setLenient(false); 
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false)); 
    binder.registerCustomEditor(State.class, new StatePropertyEditor()); 
    binder.registerCustomEditor(Category.class, new CategoryPropertyEditor()); 
    binder.registerCustomEditor(Priority.class, new PriorityPropertyEditor()); 
    binder.registerCustomEditor(User.class, "userResponsible", new UserPropertyEditor()); 
    //binder.registerCustomEditor(User.class, new UserPropertyEditor()); 
} 
... 

我缺少什麼?

+0

用戶服務不會被注入,因爲它不是一個管理的彈簧,您應該自己設置它。 –

+0

你在說什麼?在用戶屬性編輯器中? – Joe

回答

1

在你的initBinder方法中,你自己構造UserPropertyEditor,因爲這使得它成爲非彈簧管理bean,@Autowired不起作用。

要麼你必須自己調用setUserService或者你必須要求應用程序上下文爲你的電線連接實例。

@Controller 
public class TaskController { 
    @Autowired 
    private UserService userService; 

    @InitBinder 
    public void initBinder(WebDataBinder binder, HttpServletRequest req) { 
     UserPropertyEditor upe = new UserPropertyEditor(); 
     upe.setUserService(userService); 
     binder.registerCustomEditor(User.class, "userResponsible", upe); 
    } 
} 

當你自己打電話的setter,你必須在UserService注入到您的控制器,讓你有機會獲得它。現在構建一個UserPropertyEditor並調用其上的setUserService方法。

@Controller 
public class TaskController { 
    @Autowired 
    private ApplicationContext context; 

    @InitBinder 
    public void initBinder(WebDataBinder binder, HttpServletRequest req) { 
     UserPropertyEditor upe = new UserPropertyEditor(); 
     context.getAutowireCapableBeanFactory().autowireBean(upe); 
     binder.registerCustomEditor(User.class, "userResponsible", upe); 
    } 
} 

當使用ApplicationContexct你來注入到控制器中,獲得了AutowireCapableBeanFactory並調用autowireBean方法對bean的新構建的實例。現在Spring將爲你的對象做任何注入和回調。

+0

我會嘗試你說的,但現在我不明白爲什麼其他PropertyEditor類的狀態,類別和優先級似乎很好地工作? – Joe

+1

問題是沒有userService類,因此沒有空指針。其他編輯器可能沒有任何其他自動裝配依賴項或依賴項。要點是'UserPropertyEditor'沒有'UserService'就無法工作 –