With the @Autowired UserRepository repository in the UserServiceImpl class, I get NPE. Everything that was on the forum read. Nothing helped. Code:
User.java
package com.springmail.model; import javax.persistence.*; import java.io.Serializable; @Entity @Table(name = "demo_users") public class User implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; @Column(name = "chat_id") private long chatId; protected User(){ } public User(long chatId) { this.chatId = chatId; } @Override public String toString() { return super.toString(); } public long getId() { return id; } public long getChatId() { return chatId; } } UserRepository.java
package com.springmail.dao; import com.springmail.model.User; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; @Repository public interface UserRepository extends CrudRepository<User, Long> {} Application.java
package com.springmail; import com.springmail.controller.TelegramBot; import com.springmail.dao.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.context.annotation.ComponentScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.transaction.annotation.EnableTransactionManagement; @SpringBootApplication @EnableJpaRepositories(basePackages = {"com.springmail.dao"}) @ComponentScan(basePackages = {"com.springmail"}) @EntityScan @EnableTransactionManagement public class Application implements CommandLineRunner{ @Autowired UserRepository repository; //Проверил первый ответ. @Autowired UserServiceImpl service; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void run(String... strings) throws Exception { service.createUser(new User(123456789)); } } UserServiceImpl.java
package com.springmail.service; import com.springmail.model.User; import com.springmail.dao.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImp{ @Autowired UserRepository repository; //null public void createUser(User newUser) { repository.save(newUser); //NPE } public void deleteUser(User user) { repository.delete(user); } public void findUser(User user) { repository.findOne(user.getId()); } public Iterable<User> findAllUsers() { return repository.findAll(); } public void deleteAll() { repository.deleteAll(); } }