0

People, I need of a help! I'm trying to save an form data of users, but i recept this error when i submit the form in my page jsf with .

The error happen in the line 27, when i call the instruction "userService.addUser(user)" of the method save.

Bellow I will put the code and stracktrace for you look at.

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;
    }
}
Leonardo Torres
  • 385
  • 1
  • 2
  • 11

2 Answers2

0

The user property of your RegisterUserMB class is being set to null somewhere.

Sam
  • 2,350
  • 1
  • 11
  • 22
  • yes, I saw this. But I do not know how solve it. So far only discovery with the annotation @autowired don't working, and the userService is null during the call. – Leonardo Torres Aug 14 '18 at 20:45
0

You are using two different Content Dependency Injection in the same application:

Spring DI and the JSF CDI

Each of these frameworks manage its Beans Factory on it own, theirs factory are isolated one between other.

you need to specify which of them will manage the Dependency Injection.

SECOND EDIT

You must specify Spring will take the control over the injection doing two things:

1) The first one is to use @Controller and @Scope annotations instead of JSF annotations in order to just use spring beans.

2) and The second one is specify to JSF that Spring will manage the El expression language in the views

faces-config.xml

<application>
    <el-resolver>
        org.springframework.web.jsf.el.SpringBeanFacesELResolver
    </el-resolver>
</application>
Jorge L. Morla
  • 630
  • 5
  • 14
  • Oh, really? what you recomed to I use? I've been debugging my code and noticed that the @autowired annotation is not working. Soon, the useService is staying null. – Leonardo Torres Aug 14 '18 at 20:34
  • If you want to use both framework you must specify jsf that spring will manage the injection, you can achieve this adding a configuration in the faces-config.xml file – Jorge L. Morla Aug 14 '18 at 20:39
  • Great Jorge, worked perfectly! Thank you very much! – Leonardo Torres Aug 14 '18 at 21:18