英文:
NullPointer Exception Repository Spring Boot Jpa
问题
以下是您提供的代码的翻译部分:
主类:
@SpringBootApplication
public class ExampleApplication {
@Autowired
private static DataRepository dataRepository;
public static void main(String[] args) {
SpringApplication.run(ExampleApplication.class, args);
System.out.println(dataRepository.findAll());
}
}
模型:
@Entity
public class Data {
@Id
private Long id;
private String content;
public Data(Long id, String content) {
super();
this.id = id;
this.content = content;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "Data [id=" + id + ", content=" + content + "]";
}
}
仓库:
@Repository
public interface DataRepository extends JpaRepository<Data, Long> {
}
控制器:
@RestController
public class DataController {
@Autowired
private DataRepository dataRepository;
@GetMapping("/show")
public List<Data> findCities(Data data) {
return dataRepository.findAll();
}
}
错误信息:
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 启动)
2020-04-07 10:27:14.326 INFO 19892 --- [ main] com.example.demo.ExampleApplication : 未设置活动配置文件,回退到默认配置文件: default
2020-04-07 10:27:14.739 INFO 19892 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : 以默认模式引导 Spring Data JPA 仓库。
2020-04-07 10:27:14.794 INFO 19892 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : 在 48 毫秒内完成 Spring Data 仓库扫描。找到 1 个 JPA 仓库接口。
...
请注意,上述翻译可能不包括代码中的所有细节,但已涵盖您所提供的内容。如果您有任何问题或需要更多翻译,请随时告知。
英文:
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
@SpringBootApplication
public class ExampleApplication {
@Autowired
private static DataRepository dataRepository;
public static void main(String[] args) {
SpringApplication.run(ExampleApplication.class, args);
System.out.println(dataRepository.findAll());
}
}
Model
@Entity
public class Data {
@Id
private Long id;
private String content;
public Data(Long id, String content) {
super();
this.id = id;
this.content = content;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "Data [id=" + id + ", content=" + content + "]";
}
}
Repository
@Repository
public interface DataRepository extends JpaRepository<Data, Long> {
}
Controller
@RestController
public class DataController {
@Autowired
private DataRepository dataRepository;
@GetMapping("/show")
public List<Data> findCities(Data data) {
return dataRepository.findAll();
}
}
Error
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)
2020-04-07 10:27:14.326 INFO 19892 --- [ main] com.example.demo.ExampleApplication : No active profile set, falling back to default profiles: default
2020-04-07 10:27:14.739 INFO 19892 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
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.
2020-04-07 10:27:15.147 INFO 19892 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-04-07 10:27:15.261 INFO 19892 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2020-04-07 10:27:15.300 INFO 19892 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-04-07 10:27:15.363 INFO 19892 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.12.Final
2020-04-07 10:27:15.490 INFO 19892 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-04-07 10:27:15.588 INFO 19892 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL55Dialect
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)
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]
2020-04-07 10:27:16.063 INFO 19892 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
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
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)
Exception in thread "main" java.lang.NullPointerException
at com.example.demo.ExampleApplication.main(ExampleApplication.java:17)
2020-04-07 10:27:16.422 INFO 19892 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-04-07 10:27:16.424 INFO 19892 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2020-04-07 10:27:16.428 INFO 19892 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource
答案1
得分: 1
尝试一下这个:
尝试这个
@SpringBootApplication
public class ExampleApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
DataRepository dataRepository = context.getBean(DataRepository.class);
System.out.println(dataRepository.findAll());
}
}
除非您使用`postConstruct`,否则无法在静态字段上使用`autowire`。
英文:
try this
@SpringBootApplication
public class ExampleApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
DataRepository dataRepository = context.getBean(DataRepository.class);
System.out.println(dataRepository.findAll());
}
}
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论