调用存储库时出错-由以下原因引起:java.lang.NullPointerException

时间:2018-08-14 19:51:33

标签: java spring jsf

人们,我需要帮助!我正在尝试保存用户的表单数据,但是当我在页面jsf中使用提交表单时,我接受了此错误。

当我调用方法save的指令“ userService.addUser(user)”时,该错误发生在第27行。

下面我将把代码和stracktrace放给你看。

Stracktrace:

ago 14, 2018 1:28:47 PM com.sun.faces.context.AjaxExceptionHandlerImpl handlePartialResponseError
GRAVE: javax.faces.el.EvaluationException: java.lang.NullPointerException
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:491)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:764)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1388)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    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)
Caused by: java.lang.NullPointerException
    at br.com.jamming.controller.RegisterUserMB.save(RegisterUserMB.java:27)
    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)

RegisterUserMB.class

@ManagedBean
@RequestScope
public class RegisterUserMB {

    @Autowired
    private UserService userService;

    private User user = new User();
    private ProfileUser profileUser = new ProfileUser();

    private String confirmPw;

    public String save() {
        userService.addUser(user);
        return "";

    }

UserService.class

@Service
public class UserService {

    @Autowired
    UserRepository<User> userRepository;

    @Transactional
    public boolean addUser(User user) {
        return userRepository.save(user) != null;
    }
}

2 个答案:

答案 0 :(得分:0)

您的user类的RegisterUserMB属性在某处设置为null

答案 1 :(得分:0)

您在同一应用程序中使用两个不同的 Content Dependency Injection (内容依赖注入):

Spring DI JSF CDI

每个框架都自行管理其Beans Factory,它们的工厂彼此隔离。

您需要指定其中哪个将管理依赖项注入。

  

第二编辑

您必须指定Spring将通过两件事来控制注入:

1)第一个方法是使用@Controller@Scope批注而不是JSF批注,以便仅使用spring bean。

2),第二个是向JSF指定Spring将在视图中管理El表达式语言

faces-config.xml

<application>
    <el-resolver>
        org.springframework.web.jsf.el.SpringBeanFacesELResolver
    </el-resolver>
</application>