1

I have Swing application with Spring Boot and Spring Data JPA and without beans.xml (I use just annotations). So started normally. I used next annotations:

@SpringBootApplication
@ComponentScan("com.customerproject")
@EnableAutoConfiguration
public class Application { ... }

1.I have repository interface:

@Repository("profileRepository")
public interface ProfileRepository extends CrudRepository<Profile, Long> {}

2.In main application class (Application) some method I declared with annotation @Bean - here I returned new instance of main frame (MainFrame) - work correctly, It's OK:

...// annotations
public class Application {
// ... method main, DataSource and other methods
    @Bean
    public MainFrame frame() { return new MainFrame(); }
}

So, started application - showing MainFrame window.

3.In MainFrame I call new frame (CreateProfileFrame) like:

CreateProfileFrame cpf = new CreateProfileFrame();
cpf.setVisible(true);

In 'CreateProfileFrame' I declared my repository:

@Autowired
private ProfileRepository profileRepository;

And have method for data manipulation - creating Profile from repository:

Profile pr = new Profile();
pr.setEmail(emailTextField.getText());
Profile saved = profileRepository.save(profile);

But have NullPointerException in last line from this code: profileRepository.save(profile); bacause profileRepository is null.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

So question... - how can I use repository with @Autowired annotation anywhere in different classes or frames? Help, please.

Leo
  • 97
  • 2
  • 12
  • For future references, see [this solution](https://stackoverflow.com/questions/49210144/autowiring-not-working-in-springboot-application/49781415#49781415). – Carlos Nantes Apr 11 '18 at 17:57
  • it's application context related, future reference as well : https://itqna.net/questions/33008/spring-boot-autowired-jframe – Reema Mar 30 '22 at 08:42

0 Answers0