0

I am developing an JavaFx application with spring boot,JPA, and H2. I have a user entity when I try to add a new user into the DB it throws NPE in the controller on the button's click action. As it is seen I use only autowire notation. I researched but findings did not help out. Any help please?

package com.core;

@SpringBootApplication
@Import(SharedSpringConfiguration.class)
public class Runner extends Application {

   private ConfigurableApplicationContext context;

   public static void main(String[] args) {
     launch(args);        
   }

   @Override
   public void init() {
    context = SpringApplication.run(Runner.class);
   }
}
package com.dao;

@Entity
@Table(name = "user")
public class User {

    @Id
    @Column(name = "id", updatable = false, nullable = false)
    private long ID;
    @Column(nullable = false)
    private String userName;
    @Column(nullable = false)
    private String userPass;

    public User() {
    }

    public User(long ID, String userName, String userPass) {
        this.ID = ID;
        this.userName = userName;
        this.userPass = userPass;
    }
}
package com.service;

@Service
public class UserService {

   @Autowired
   private UserRepository userRepository;

   public UserService() {
   }

   public void saveUser(User user) {
     userRepository.save(user);
   }
}
package com.repository;
public interface UserRepository extends CrudRepository<User, Long> {}
package com.controller

@Controller
public class MethodController implements Initializable {

  @Autowired
  private UserService userService;

  @FXML
  void methodSave(MouseEvent event) {
    userService.saveUser(new User(11, "TestUser", "noPass")); //Throws NPE. Indicates that userService is null. But I autowire the userService.
  }
}
6
  • In your UserService class you try and autowire UserRepository. UserRepository is not setup to be a spring component. Try adding @Repository to your UserRepository class. Commented Jul 23, 2018 at 20:50
  • @gonzo, I already tried that. It does not help Commented Jul 23, 2018 at 21:00
  • Which @Repository tag did you try? You won't be able to autowire UserRepository without it being scanned by spring. Commented Jul 23, 2018 at 21:02
  • @gonzo, org.springframework.stereotype.Repository; Commented Jul 23, 2018 at 21:11
  • 2
    @gonzo an @Repository annotation is not necessary. It is inherited from the CrudRepository Commented Jul 24, 2018 at 6:05

2 Answers 2

2

I don't know what's in SharedSpringConfiguration, but you probably need @EnableJpaRepositories on one of your configuration classes. @Repository on the CrudRepo should be unnecessary.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for bringing it up. My bad I made an implementation mistake in SharedConfig... Now the question what is the way to use the UserService service = Runner.context.getBean(UserService.class); in my controller. I mean how to define it? like make the context as public static in the Runner and reach it from any controller or what is the way that it should be done?
After probing we can accomplish it via ApplicationContextProvider
0

Change your SpringBootApplication package from com.core to com

because SpringBootApplication by default will scan only that packages and sub packages.

else

add @ComponentScan annotation in SpringBootApplication and scan the packages.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.