#Getting NPE from autowired UserService (Spring MVC + S. Data JPA + Hibernate)

39 messages · Page 1 of 1 (latest)

flat beacon
#

Hello everyone! I have a little problem, so I'll be sincerely thankful if someone will help me with it.
It's not spring boot but ordinary Spring MVC + Spring Data JPA + Hibernate + MySQL.

https://stackoverflow.com/questions/77802239/getting-nullpointerexception-from-my-service-class-spring-mvc-spring-data-jpa

Also github with this project:

https://github.com/avevivi/task2.2.3

GitHub

Contribute to avevivi/task2.2.3 development by creating an account on GitHub.

unborn beaconBOT
#

This post has been reserved for your question.

Hey @flat beacon! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

flat beacon
#

And folder structure:

lapis terrace
#

and calling the controllers

#

anyway try changing usercontroller to just do:


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

without the no-args constructor

flat beacon
flat beacon
#

If I don't put NoArgsConstructor I keep getting Bean creating error

#

on controller

lapis terrace
#

should just be all args constructor+autowired

lapis terrace
flat beacon
#

like this

lapis terrace
#

ah ok

lapis terrace
#

or you haven't got that far?

flat beacon
#

oh actually there no /allUsers

#

it's just "/"

#

like locaholst first page when you run server

#

also "Application context" in tomcat deployment has to be empty

#

or /

#

so it wont redirect

#

to project name

lapis terrace
#

try changing the versions:
spring data jpa: 3.2.1 -> 2.7.18
spring: 5.3.14 -> 5.3.31
and change java version to 17 (optional)

#

i also moved everything from src/main/webapp to src/main/java (apart from pages) not sure if you will need to do that too (that's the maven standard)

#

and usercontroller can just have:

    public UserController(UserService userService) {
        this.userService = userService;
    }

no autowired, no no-args, etc

flat beacon
#

holy it's actually working

#

i think the main problem was structure

#

i've missed that I made wrong structure

#

Thank you so much

unborn beaconBOT
# flat beacon Thank you so much

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

flat beacon
#

@lapis terrace You are lifesaver