1
Guys, I’m going through a problem here, and I can’t solve it. I’ve looked in the forums, but I haven’t seen any solutions either. Anyone gives this help?
The error happens when I try to save the folder with the user data. I thought I might be sending the null user object, but I’ve verified that’s not it.
I have checked many times to see if I forgot to initialize some instance or object variable, but also found nothing wrong.
stracktrace indicates that the error is on line 27, in the call serviceUser.adduser(user);
Caused by: java.lang.NullPointerException
at br.com.jamming.controller.RegisterUserMB.save(RegisterUserMB.java:27)
Does anyone know what may be going on? I appreciate the availability.
Magagedbean:
@ManagedBean
@RequestScope
public class RegisterUserMB {
@Autowired
private UserService userService;
private User user = new User();
private String confirmPw;
public String save() {
userService.addUser(user);
return "";
}
Service:
@Service
public class UserService {
@Autowired
UserRepository<User> userRepository;
@Transactional
public boolean addUser(User user) {
return userRepository.save(user) != null;
}
Repository:
public interface UserRepository<U> extends CrudRepository<User, Long> {
}
xhtml:
<h:form>
<h:panelGrid columns="2" style="border:none">
<p:inputText id="name" value="#{registerUserMB.profileUser.name}" placeholder="Name" />
<p:inputText id="surname" value="#{registerUserMB.profileUser.surname}" placeholder="Surname" />
</h:panelGrid>
<h:panelGrid>
<p:inputText id="mail" value="#{registerUserMB.user.mail}" placeholder="E-mail" size="46" />
</h:panelGrid>
<h:panelGrid columns="2">
<p:password id="password" value="#{registerUserMB.user.password}" feedback="true" inline="true" placeholder="Password" match="PasswordConf" />
<p:password id="PasswordConf" value="#{registerUserMB.confirmPw}" placeholder="Password confirmation"/>
<p:calendar id="brithday" value="#{registerUserMB.profileUser.dateOfBrithday}" placeholder="Brithday" /><h:outputLabel value="" />
<p:inputText id="cellphone" value="#{registerUserMB.user.cellphone}" placeholder="Cellphone"/>
</h:panelGrid>
<p:commandButton value="Save" actionListener="#{registerUserMB.save}"/>
</h:form>
Thank you!!
Line 27 is
userService.addUser(user);
?userService
is not null? Are you sure thatRegisterUserMB
is managed and is in the context of spring?– Bruno César
Bruno speaks, I was able to solve, userService was to be managed by the spring context, through the @autoWired annotation, but for this to happen it is necessary a configuration that integrates spring with JSF, in faces-config. [https://www.concretepage.com/spring-4/spring-jsf-2-integration-example-using-autowired-annotation] thanks for your attention!
– Leonardo Torres
userService was, which is not the JSF bean, which is what I said there, so obviously spring couldn’t inject the dependency, because it didn’t know Registerusermb ;)
– Bruno César