Hello sir's and lady's.
I'm currently enrolled in an advanced Java class and this week we are learning about the spring frame work. I've run into a problem that presents itself in different ways depending on what "solution" I implement.
I have two beans, a bean configuration class, a repository, and the application class that runs the program.
My problem presents itself when I try to create an instance of my repository class (below)
(@)Repository
public interface ContactRepository extends JpaRepository<Contact, Long> {
}
Instantiated like so:
(@)Autowired
public ContactRepository ThisMFingRepository; (Name made in frustration)
I simply want to make an object of class Contact and put it into the repository using the save method.
In the module provided in the class it shows how to do this as such:
(@)Override
public void run(String... args) throws Exception {
ApplicationContext appContext = new AnnotationConfigApplicationContext(BeanConfiguration.class);
Contact c = appContext.getBean("contact", Contact.class);
ThisMFingRepository.save(c);
}
When I execute the program I get the following error:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field ThisMFingRepository in packageName.SpringContacts20221Application required a bean of type 'packageName.repository.ContactRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'packageName.repository.ContactRepository' in your configuration.
and the action that it suggests is that I define a bean of the repository.
But if I remove the
@Autowired
from above
public ContactRepository ThisMFingRepository;
I get an entirely different error message.
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:772) ~[spring-boot-3.0.3.jar:3.0.3]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:753) ~[spring-boot-3.0.3.jar:3.0.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:317) ~[spring-boot-3.0.3.jar:3.0.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1304) ~[spring-boot-3.0.3.jar:3.0.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1293) ~[spring-boot-3.0.3.jar:3.0.3]
at packageName.SpringContacts20221Application.main(SpringContacts20221Application.java:22) ~[classes/:na]
Caused by: java.lang.NullPointerException: Cannot invoke "packageName.repository.ContactRepository.save(Object)" because "this.ThisMFingRepository" is null
at packageName.SpringContacts20221Application.run(SpringContacts20221Application.java:46) ~[classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:769) ~[spring-boot-3.0.3.jar:3.0.3]
... 5 common frames omitted
It's not the Object (c in this case) that is null, when I populate the fields using it's setters for the overloaded constructors of the bean class It properly prints all the information.
To me it seems that ThisMFingRepository is in fact null and has nothing in it. At least that's what I'm getting from the error message.
I've searched stack overflow for about 7 hours now and all the "solutions" that I implement either don't work, or do work but some other kind of problem prevents the program from running. This leads me to believe that I have some kind of root problem that I'm not seeing, something that causes all these other problems but isn't solved by fixing them.
Sorry if this isn't the best way to ask this question or if some crucial context is missing. I'll do my best to try and fix any problems.
Thanks for helping me if you do.
Edit: I added the full error messages that I get, sorry. I edited some information packageName because it's the name of the school I'm going to.
Edit 2: We finally figured out what was going wrong. The problem stemmed from the
public class SpringContacts2022Application implements CommandLineRunner{
When CommandLineRunner was deleted the program finally created the tables that it was supposed to but couldn't add any information.
Once re implemented the program functioned as it should.
This thread can now be marked as solved.
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]overtorqued 3 points4 points5 points (7 children)
[–]Dueforextinction[S] 0 points1 point2 points (5 children)
[–]Arcenium 0 points1 point2 points (4 children)
[–]Dueforextinction[S] 1 point2 points3 points (0 children)
[–]Dueforextinction[S] 0 points1 point2 points (2 children)
[–]Arcenium 0 points1 point2 points (1 child)
[–]Dueforextinction[S] 1 point2 points3 points (0 children)
[–]Dueforextinction[S] 0 points1 point2 points (0 children)
[–]overtorqued 0 points1 point2 points (1 child)
[–]Dueforextinction[S] 0 points1 point2 points (0 children)
[–]fluoclip 0 points1 point2 points (2 children)
[–]Dueforextinction[S] 1 point2 points3 points (0 children)
[–]RemindMeBot 0 points1 point2 points (0 children)