DB connection always rises error

I’m following this tutorial https://www.javadevjournal.com/spring-boot/spring-boot-mysql/

And I checked 14th time + already and I always get
java.lang.NullPointerException: Cannot invoke “com.test.db.UserRepository.findAll()” because “this.userRepository” is null

pastebin with most of the code: https://pastebin.com/9tpZuwDa

I’m on Vaadin 24,
with the following application.properties
logging.level.org.atmosphere = warn
spring.mustache.check-template-location = false

Launch the default browser when starting the application in development mode


To improve the performance during development.

For more information https://vaadin.com/docs/flow/spring/tutorial-spring-configuration.html#special-configuration-parameters

vaadin.whitelisted-packages = com.vaadin,org.vaadin,dev.hilla,com.spectral369
spring.jpa.defer-datasource-initialization = true

spring.datasource.url = jdbc:mysql://localhost:3306/pdv_apia
spring.datasource.username = root
spring.datasource.password = test01
spring.datasource.tomcat.time-between-eviction-runs-millis = 60000


Can anyone help me with this matter ?

There are crucial information missing, like the package name and your code.

I left the package name out on purpose, I didn’t select the line

I think I’ll just recreate the project and copy paste my extra ~20 classes, maybe then it will work

A wild guess: are you perhaps invoking the method on the repository in the constructor of the class with autowired annotation?

No, but I tried that too lol

I just re created the project from a start template and now I’ll try to run a simple example to see if it runs :slightly_smiling_face:

well it doesn;t work

still the same issue

but this time the DB tables are created !


Please post the class where the issue occured.

here you have one pastebin; and here is another https://pastebin.com/mu6KD3eu

Please use constructor injection

Autowiring fields it discouraged and therefore can’t be accessed within the constructor


My guess was correct :slightly_smiling_face:
As knoobie suggested, use constructor injection instead of autowiring fields

Why are you going me on older version from my tutorial link ?

Ok, I’ll try this… re create everything again. but even this won’t work, I’ll just make the classic singleton ConnDB class and use that !


Just change a single line of code and you are good to go.