DB connection always rises error

Hello,
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
server.port=${PORT:8080}
logging.level.org.atmosphere = warn
spring.mustache.check-template-location = false

Launch the default browser when starting the application in development mode

vaadin.launch-browser=true

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

#DB
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
spring.datasource.tomcat.max-idle=1

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
logging.level.com.zaxxer.hikari=DEBUG
spring.jpa.show-sql=true

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 !

help

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

https://www.baeldung.com/constructor-injection-in-spring

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 !

Nooo

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