空指针异常 仓库 Spring Boot JPA

huangapple go评论110阅读模式
英文:

NullPointer Exception Repository Spring Boot Jpa

问题

以下是您提供的代码的翻译部分:

主类:

  1. @SpringBootApplication
  2. public class ExampleApplication {
  3. @Autowired
  4. private static DataRepository dataRepository;
  5. public static void main(String[] args) {
  6. SpringApplication.run(ExampleApplication.class, args);
  7. System.out.println(dataRepository.findAll());
  8. }
  9. }

模型:

  1. @Entity
  2. public class Data {
  3. @Id
  4. private Long id;
  5. private String content;
  6. public Data(Long id, String content) {
  7. super();
  8. this.id = id;
  9. this.content = content;
  10. }
  11. public Long getId() {
  12. return id;
  13. }
  14. public void setId(Long id) {
  15. this.id = id;
  16. }
  17. public String getContent() {
  18. return content;
  19. }
  20. public void setContent(String content) {
  21. this.content = content;
  22. }
  23. @Override
  24. public String toString() {
  25. return "Data [id=" + id + ", content=" + content + "]";
  26. }
  27. }

仓库:

  1. @Repository
  2. public interface DataRepository extends JpaRepository<Data, Long> {
  3. }

控制器:

  1. @RestController
  2. public class DataController {
  3. @Autowired
  4. private DataRepository dataRepository;
  5. @GetMapping("/show")
  6. public List<Data> findCities(Data data) {
  7. return dataRepository.findAll();
  8. }
  9. }

错误信息:

  1. 2020-04-07 10:27:14.323 INFO 19892 --- [ main] com.example.demo.ExampleApplication : 启动 ExampleApplication,进程ID 19892(位于 C:\Users\David\Documents\workspace-spring-tool-suite-4-4.5.1.RELEASE\example\target\classes,由 David C:\Users\David\Documents\workspace-spring-tool-suite-4-4.5.1.RELEASE\example 启动)
  2. 2020-04-07 10:27:14.326 INFO 19892 --- [ main] com.example.demo.ExampleApplication : 未设置活动配置文件,回退到默认配置文件: default
  3. 2020-04-07 10:27:14.739 INFO 19892 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : 以默认模式引导 Spring Data JPA 仓库。
  4. 2020-04-07 10:27:14.794 INFO 19892 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : 48 毫秒内完成 Spring Data 仓库扫描。找到 1 JPA 仓库接口。
  5. ...

请注意,上述翻译可能不包括代码中的所有细节,但已涵盖您所提供的内容。如果您有任何问题或需要更多翻译,请随时告知。

英文:

I am making a basic application to send an insert to a database, as a test I have been executing a simple findAll () to test the repository, but it always gives me NullPointer
I am aware that it is something very basic and also that I am missing something.
Thank you.

This is the main

  1. @SpringBootApplication
  2. public class ExampleApplication {
  3. @Autowired
  4. private static DataRepository dataRepository;
  5. public static void main(String[] args) {
  6. SpringApplication.run(ExampleApplication.class, args);
  7. System.out.println(dataRepository.findAll());
  8. }
  9. }

Model

  1. @Entity
  2. public class Data {
  3. @Id
  4. private Long id;
  5. private String content;
  6. public Data(Long id, String content) {
  7. super();
  8. this.id = id;
  9. this.content = content;
  10. }
  11. public Long getId() {
  12. return id;
  13. }
  14. public void setId(Long id) {
  15. this.id = id;
  16. }
  17. public String getContent() {
  18. return content;
  19. }
  20. public void setContent(String content) {
  21. this.content = content;
  22. }
  23. @Override
  24. public String toString() {
  25. return &quot;Data [id=&quot; + id + &quot;, content=&quot; + content + &quot;]&quot;;
  26. }
  27. }

Repository

  1. @Repository
  2. public interface DataRepository extends JpaRepository&lt;Data, Long&gt; {
  3. }

Controller

  1. @RestController
  2. public class DataController {
  3. @Autowired
  4. private DataRepository dataRepository;
  5. @GetMapping(&quot;/show&quot;)
  6. public List&lt;Data&gt; findCities(Data data) {
  7. return dataRepository.findAll();
  8. }
  9. }

Error

  1. 2020-04-07 10:27:14.323 INFO 19892 --- [ main] com.example.demo.ExampleApplication : Starting ExampleApplication on DESKTOP-ARNRCSA with PID 19892 (C:\Users\David\Documents\workspace-spring-tool-suite-4-4.5.1.RELEASE\example\target\classes started by David in C:\Users\David\Documents\workspace-spring-tool-suite-4-4.5.1.RELEASE\example)
  2. 2020-04-07 10:27:14.326 INFO 19892 --- [ main] com.example.demo.ExampleApplication : No active profile set, falling back to default profiles: default
  3. 2020-04-07 10:27:14.739 INFO 19892 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
  4. 2020-04-07 10:27:14.794 INFO 19892 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 48ms. Found 1 JPA repository interfaces.
  5. 2020-04-07 10:27:15.147 INFO 19892 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
  6. 2020-04-07 10:27:15.261 INFO 19892 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
  7. 2020-04-07 10:27:15.300 INFO 19892 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
  8. 2020-04-07 10:27:15.363 INFO 19892 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.12.Final
  9. 2020-04-07 10:27:15.490 INFO 19892 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
  10. 2020-04-07 10:27:15.588 INFO 19892 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL55Dialect
  11. 2020-04-07 10:27:15.832 INFO 19892 --- [ main] org.hibernate.tuple.PojoInstantiator : HHH000182: No default (no-argument) constructor for class: com.example.demo.Data (class must be instantiated by Interceptor)
  12. 2020-04-07 10:27:16.057 INFO 19892 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
  13. 2020-04-07 10:27:16.063 INFO 19892 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit &#39;default&#39;
  14. 2020-04-07 10:27:16.066 INFO 19892 --- [ main] f.a.AutowiredAnnotationBeanPostProcessor : Autowired annotation is not supported on static fields: private static com.example.demo.DataRepository com.example.demo.ExampleApplication.dataRepository
  15. 2020-04-07 10:27:16.417 INFO 19892 --- [ main] com.example.demo.ExampleApplication : Started ExampleApplication in 2.399 seconds (JVM running for 3.041)
  16. Exception in thread &quot;main&quot; java.lang.NullPointerException
  17. at com.example.demo.ExampleApplication.main(ExampleApplication.java:17)
  18. 2020-04-07 10:27:16.422 INFO 19892 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit &#39;default&#39;
  19. 2020-04-07 10:27:16.424 INFO 19892 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
  20. 2020-04-07 10:27:16.428 INFO 19892 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource

答案1

得分: 1

尝试一下这个:

  1. 尝试这个
  2. @SpringBootApplication
  3. public class ExampleApplication {
  4. public static void main(String[] args) {
  5. ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
  6. DataRepository dataRepository = context.getBean(DataRepository.class);
  7. System.out.println(dataRepository.findAll());
  8. }
  9. }
  10. 除非您使用`postConstruct`否则无法在静态字段上使用`autowire`
英文:

try this

  1. @SpringBootApplication
  2. public class ExampleApplication {
  3. public static void main(String[] args) {
  4. ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
  5. DataRepository dataRepository = context.getBean(DataRepository.class);
  6. System.out.println(dataRepository.findAll());
  7. }
  8. }

you can not use autowire with static field unless you use postConstruct

答案2

得分: 0

Jpa 需要默认构造函数,因此将其放入你的数据类中。

英文:

Jpa needs defaut constructor so put it into your Data class

huangapple
  • 本文由 发表于 2020年4月7日 16:13:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/61075571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定